SQL Operators and Operands 

An expression is value combined with a symbol to produce a new value. To make this possible, an expression may involve at least one value, the name of a column or else with one of the operators we have used in the previous lesson or one of those we will review here. There is no specific formula to follow: The formulation of an expression depends on the operator(s) and the operand(s) involved. To use an expression in your Windows Forms Application, you can write one of the expressions we will studying, pass it to a SqlCommand object, and execute it.
Here is another example:
SELECT +1250

The order you use to add two or more values doesn't matter. This means Value1 + Value2 is the same as Value2 + Value1. In the same way a + b + c is the same as a + c + b the same as b + a + c and the same as c + b + a. This means that the addition is associative. 
The Subtraction 
The subtraction operation, sometimes called the difference, is used to take out or subtract one value from another value. It is essentially the opposite of the addition. The subtraction is performed with the  sign. Here is an example: 
SELECT 1240  608 
Unlike the addition, the subtraction operation is not associative. This means that a  b  c is not necessarily equal to c  b  a. This is illustrated in the following statements: 
SELECT 128  42  5 SELECT 5  42  128 
Notice that both operations of the addition convey the same result. In the subtraction section, the numbers follow the same order but a different operation; and the last two operations render different results. 
The Multiplication * 
The multiplication can be used to multiply each value of a column to a constant number. You can also use it to multiply the value of one column to the corresponding value of another column. Imagine you have created a table used to hold payroll information for employees as follows:
Imagine a few employees have submitted their times as follows:
To get the weekly salary of each employee, you can multiply the WeeklyHours column by the HourlySalary and get the result. Here is an example: 
The Division 
The division operation is similar to cutting an item in pieces or fractions of a set value. Therefore, the division is used to get the fraction of one number in terms of another. The division is performed with the forward slash /. Here is an example: SELECT 128 / 42 This would produce 3 When performing the division, be aware of its many rules. Never divide by zero (0). Make sure that you know the relationship(s) between the numbers involved in the operation. 
The Modulo 
In the above division, 128/42, the result is 3. When you multiply 42 by 3, as in 42*3, you get 126. In some cases, you may be interested in knowing the amount that was left out after the operation. The modulo operation is used to get the remainder of a division as a natural number. The remainder operation is performed with the percent sign (%). Here is an example: PRINT 128 % 42 This would produce 2 
Parentheses 
Like most computer languages, TransactSQL uses parentheses to isolate a group of items that must be considered as belonging to one entity. For example, as we will learn soon, parentheses allow a function to delimit the list of its arguments. Parentheses can also be used to isolate an operation or an expression with regards to another operation or expression. For example, when studying the algebraic operations, we saw that the subtraction is not associative and can lead to unpredictable results. In the same way, if your operation involves various operators such as a mix of addition(s) and subtraction(s), you can use parentheses to specify how to proceed with the operations, that is, what operation should (must) be performed first. Here is an example: 
PRINT (154  12) + 8 PRINT 154  (12 + 8) 
This would produce:
150 134 
As you can see, using the parentheses controls how the whole operation would proceed. This difference can be even more accentuated if your operation includes 3 or more operators and 4 or more operands. 
Bit Manipulations 
Introduction 
When you use a value in your database or application, the
value must be stored somewhere in the computer memory using a certain amount of space.
As we review in our study of bytes and
words, a value occupies space that resembles a group of small boxes. In
our human understanding, it is not always easy to figure out how a letter
such as as B is stored in 7 seven small boxes when we know that B is only
one letter. 
Bits Operators: The Bitwise NOT Operator ~ 
One of the operations you can perform on a bit consists of reversing its value. That is, if a bit holds a value of 1, you may want to change it to 0 and viceversa. This operation can be taken care of by the bitwise NOT operator that is represented with the tilde symbol ~ The bitwise NOT is a unary operator that must be placed on the left side of its operand as in ~Value Here is an example: PRINT ~158 To perform this operation, the TransactSQL interpreter considers each bit that is part of the operand and inverts the value of each bit from 1 to 0 or from 0 to 1 depending on the value the bit is holding. This operation can be resumed in the following table: 

Consider a number with a byte value such as 248. In our study of numeric systems, we define how to convert numbers from one system to another. Based on this, the binary value of decimal 248 is 1111 1000 (and its hexadecimal value is 0xF8). If you apply the bitwise NOT operator on it to reverse the values of its bits, you would get the following result: 

Bits Comparison: The Bitwise AND Operator & 
The bitwise & is a binary operator that uses the following syntax Operand1 & Operand2 This operator considers two values and compares the bit of each with the corresponding bit of the other value. If both corresponding bits are 1, the comparison produces 1. Otherwise, that is, if either bit is 0, the comparison produces 0. This comparison is resumed as follows: 

Imagine you have two byte values represented as 187 and 242. Based on our study of numeric systems, the binary value of decimal 187 is 1011 1011 (and its hexadecimal value is 0xBB). The binary value of decimal 242 is 1111 0010 (and its hexadecimal value is 0xF2). Let’s compare these two values bit by bit, using the bitwise AND operator: 

Most of the times, you will want the interpreter to perform this operation and use the result in your program. This means that you can get the result of this operation and possibly display it to the user. The above operation can be performed by the following program: 
PRINT 187 & 242
This would produce 178
Bits Comparison: The Bitwise OR Operator  
You can perform another type of comparison on bits using the bitwise OR operator that is represented by . Its syntax is: Value1  Value2 Once again, the interpreter compares the corresponding bits of each operand. If at least one of the equivalent bits is 1, the comparison produces 1. The comparison produces 0 only if both bits are 0. This operation is resumed as follows: 

Once again, let’s consider decimals 187 and 242. Their bitwise OR comparison would render the following result: 

You can also let the compiler perform the operation and produce a result. Here is an example: 
PRINT 187  242
This would produce 251
Bits Comparison: The BitwiseExclusive XOR Operator ^ 
Like the previous two operators, the bitwiseexclusive OR operator performs a bit comparison of two values. It syntax is: Value1 ^ Value2 The compiler compares the bit of one value to the corresponding bit of the other value. If one of the bits is 0 and the other is 1, the comparison produces 1. In the other two cases, that is, if both bits have the same value, the comparison produces 0. This operation is resumed as follows: 

We will again consider decimals 187 and 242. Their bitwiseexclusive XOR comparison would render the following result: 

If the interpreter performs this operation, it can produce a result as in the following example: 
PRINT 187 ^ 242; 
This would produce 73 
Logical Operators 
Negation Operator: NOT 
In reverse, you may want to use a rule that exclude your criterion and produces only those records that don't follow the rule. For example, you may want to see the list of your customers but excluding those who live in Mexico. To negate a condition, you can start it with the NOT operator. The condition in this case would become NOT (Country = 'Mexico'). Here is an example:
The comparison and the negation operators we reviewed above usually expect exact matches during their comparisons. For example, when a WHERE condition is formulated as Country = 'Mexico', the interpreter would look for a string under the Country column that exactly display Mexico and Mexico only. In some cases, you know know only the starting characters of a string, only the middle characters of a string, or only the ending characters of a string. The SQL allows you to look for a string that begins, contains, or ends with a few characters. The character or group of characters you provide is referred to as a pattern. To use a pattern as criterion to look for in the values of a column, you can use the LIKE operator in your WHERE condition. The formula used by this operator is: ExpressionToMatch LIKE Pattern The ExpressionToMatch placeholder of this formula can be the name of a column whose values would be examined. The Pattern placeholder is an expression that would be looked for. To create a pattern, the LIKE operator uses wildcards. A wildcard is a placeholder in which you provide one or more characters. One of the wildcards used to create a pattern is %. This is used as a placeholder for any character. For example, imagine that you want to find a list of your customers who live in a city whose name starts with M. In this case, any (number of) characters on the right side of M can be represented with the % placeholder. The condition would be formulated as follows: SELECT * FROM Customers WHERE City LIKE 'M%' In the same way, you can use the % wildcard to specify the starting or the middle characters of a string. Another wildcard used is the underscore _. Like the % wildcard, the underscore character is used as a placeholder but for only one character. Here is an example: Instead of the underscore, you can use a combination of the opening and the closing square brackets [] as a placeholder for a single character. While the underscore placeholder is used for any character, the difference with the square brackets is that, this time, you can specify the particular alphabeticbased ranged of characters you want to use. To specify the range or characters, between the square brackets, type both ending letters of the range separated by a dash character as in [ae]. Another variance of the single character placeholder is the ^ used between the square brackets. It is used to exclude characters of the specified range. An example would be [^ae]. This would produce a list of strings that don't start with either a, b, c, d, or e. You can use combinations of these wildcards to create more precise patterns. To negate a condition resulting from a LIKE pattern, you can use the NOT operator on the left side of LIKE as in NOT LIKE.

Logical Conjunction: The AND Operator 
Imagine that you are reviewing the students’ records of a school and you want to make sure that you know whom to contact in case of emergency for each student. For example, imagine that you are trying to make sure the record of each student provides an emergency telephone number. The statement to evaluate would be:
If the record of the student referred to contains an emergency phone number, the statement is true. If the record does not provide this information, our goal is not met and the statement is false. Imagine that, on the other hand, we want to find out if the record of each student displays the name to contact in case of emergency. The statement considered would be:
If the record considered displays an emergency name, the statement is true. Imagine that a student's record indicates an emergency telephone number but does not provide an emergency name. What if a student's record indicates an emergency name but no emergency telephone number? It appears that in the absence of one of these pieces of information, it would be hard to perform an effective contact. To make a record complete or to accomplish our goal, we want each record to have both an emergency name and an emergency telephone number. We can combine both of the above statements as follows:
Evaluating the combined statement would produce the following results:
To resume, our goal is met only if BOTH the first and the second statement are TRUE. This is done using the AND operator. 
Logical Disjunction: The OR Operator 
Imagine that our main goal is to make sure that we have a number we can use to contact somebody for the student. In this case, either the home phone number or the emergency number would be fine. We already have the following two statements:
To get either number, we can concatenate these two statements and evaluate the truthfulness of either one of them. We create a new statement as follows: "This student's record indicates a home phone number" OR "This student's record provides an emergency phone number" The comparative evaluation produces the following results:
The logical disjunction shows us that a combined statement is true if EITHER of its two statements IS TRUE. The logical disjunction produces a false result if BOTH of its two statements are FALSE. To perform a logical disjunction, you can use the OR operator. 
Range Test: BETWEEN 
If you have a logical range of values and you want to know if a certain exact value is contained in that range, you use the BETWEEN operator. The BETWEEN operator is usually combined with the AND operator to get a list of records between two values. The basic formula of this operator is: Expression BETWEEN Start AND End The Expression placeholder of our formula is usually the name of the column whose values you want to examine. The Start factor is the starting value of the range to consider. The End factor is the highest value to consider in the range. After this condition is executed, it produces the list of values between Start and End. As an example, if you are creating some groups of students for activities, imagine that one group would have students from ID 54 to 72. You would write the WHERE condition as follows: SELECT StudentID, StudentNbr, Gender, LastName, FirstName, DateOfBirth FROM Students WHERE StudentID BETWEEN 54 AND 72 
The IS Operator 
To validate something as being possible, you can use the IS operator. For example, to acknowledge that something is NULL, you can use the IS NULL expression. In the same way, to validate that something is not null, you can use the expression IS NOT NULL. Here is an example that displays the list of students whose records don't show the emergency name: 
Comparison Operators: IN 
If you have a series of records and want to find a record or a group of records among them, you can use the IN operator. SQL provides many and many other operators that we have not reviewed here. 


Previous  Copyright © 20052016, FunctionX, Inc.  Next 
