![]() | ![]() | ![]() | ![]() |
9.2. Comparison OperatorsThe usual comparison operators are available, shown in Table 9-1. Table 9-1. Comparison Operators
Comparison operators are available for all data types where this makes sense. All comparison operators are binary operators that return values of type boolean; expressions like 1 < 2 < 3 are not valid (because there is no < operator to compare a Boolean value with 3). In addition to the comparison operators, the special BETWEEN construct is available. a BETWEEN x AND y is equivalent to a >= x AND a <= y Similarly, a NOT BETWEEN x AND y is equivalent to a < x OR a > y There is no difference between the two respective forms apart from the CPU cycles required to rewrite the first one into the second one internally. BETWEEN SYMMETRIC is the same as BETWEEN except there is no requirement that the argument to the left of AND be less than or equal to the argument on the right; the proper range is automatically determined. To check whether a value is or is not null, use the constructs expression IS NULL expression IS NOT NULL or the equivalent, but nonstandard, constructs expression ISNULL expression NOTNULL Do not write expression = NULL because NULL is not "equal to" NULL. (The null value represents an unknown value, and it is not known whether two unknown values are equal.) This behavior conforms to the SQL standard.
The ordinary comparison operators yield null (signifying "unknown") when either input is null. Another way to do comparisons is with the IS [ NOT ] DISTINCT FROM construct: expression IS DISTINCT FROM expression expression IS NOT DISTINCT FROM expression For non-null inputs, IS DISTINCT FROM is the same as the <> operator. However, when both inputs are null it will return false, and when just one input is null it will return true. Similarly, IS NOT DISTINCT FROM is identical to = for non-null inputs, but it returns true when both inputs are null, and false when only one input is null. Thus, these constructs effectively act as though null were a normal data value, rather than "unknown". Boolean values can also be tested using the constructs expression IS TRUE expression IS NOT TRUE expression IS FALSE expression IS NOT FALSE expression IS UNKNOWN expression IS NOT UNKNOWN These will always return true or false, never a null value, even when the operand is null. A null input is treated as the logical value "unknown". Notice that IS UNKNOWN and IS NOT UNKNOWN are effectively the same as IS NULL and IS NOT NULL, respectively, except that the input expression must be of Boolean type. |
||||||||||||||||||||||||||