Home

SQL Expressions: Logical Comparisons

  

Introduction

For your databases, you can create expressions that represent a combination of values, variables, and operators. To support expressions, Transact-SQL provides operators other than, or in addition to, those we saw in Lesson 2.

A comparison is a Boolean operation that produces a true or a false result, depending on the values on which the comparison is performed. A comparison is performed between two values of the same type. For example, you can compare two numbers, two characters, or the names of two cities. To support comparisons, Transact-SQL provides all necessary operators.

     

Logical Operators

To compare two values for equality, use the = operator. Its formula is:

Value1 = Value2

The equality operation is used to find out whether two values are the same. From the above formula, the SQL interpreter would compare Value1 and Value2. If Value1 and Value2 are the same, the comparison produces a TRUE result. If they are different, the comparison renders FALSE.

As opposed to equality, to find out if two values are not equal, use the <> operator. Its formula is:

Value1 <> Value2

The <> is a binary operator (like all logical operators) that is used to compare two values. The values can come from two variables as in Variable1 <> Variable2. Upon comparing the values, if both hold different values, the comparison produces a TRUE. Otherwise, the comparison renders FALSE or a null value.

Like C#, in SQL, the comparison for a lower value is performed using the < operator. Its formula is:

Value1 < Value2

The value held by Value1 is compared to that of Value2. As it would be done with other operations, the comparison can be made between two variables, as in Variable1 < Variable2.

As done in C#, to compare for lesser or equal value in SQL, use the <= operator whose formula is:

Value1 <= Value2

If both Value1 and Value2 are the same, the result is true or positive. If the left operand, in this case Value1, holds a value lower than the second operand, in this case Value2, the result is still true. If the value of Value1 is strictly higher than that of Value, the comparison produces a FALSE result.

Like C#, the comparison for a strict greater value is performed with the > operator. Its formula is:

Value1 > Value2

Both operands, in this case Value1 and Value2, can be variables or the left operand can be a variable while the right operand is a constant. If the value on the left of the > operator is greater than the value on the right side or a constant, the comparison produces a true or positive value. Otherwise, the comparison renders false or null.

Like C#, the SQL supports the comparison for greater or equality using the >= operator. Its formula is:

Value1 >= Value2

The comparison is performed on both operands: Value1 and Value2. If the value of Value1 and that of Value2 are the same, the comparison produces a true or positive value. If the value of the left operand is greater than that of the right operand, the comparison produces true or positive also. If the value of the left operand is strictly less than the value of the right operand, the comparison produces a false or null result. 

 

Published on Monday 27 December 2007

 

Home Copyright © 2007 FunctionX, Inc. Next