DB2 10.5 for Linux, UNIX, and Windows
General comparisons
A general comparison compares two sequences of any length to determine whether at least one item in the first sequence and one item in the second sequence satisfy the specified comparison. The general comparison operators are =, !=, <, <=, >, and >=.
The following table describes these operators.
Operator | Purpose |
---|---|
= | Returns true if some value in the first sequence is equal to some value in the second sequence. |
!= | Returns true if some value in the first sequence is not equal to some value in the second sequence. |
< | Returns true if some value in the first sequence is less than some value in the second sequence. |
<= | Returns true if some value in the first sequence is less than or equal to some value in the second sequence. |
> | Returns true if some value in the first sequence is greater than some value in the second sequence. |
>= | Returns true if some value in the first sequence is greater than or equal to some value in the second sequence. |
As illustrated in the Examples section, later, general comparisons are not transitive and note that the = and != operators are not inverses of each other.
The
result of a general comparison is either a boolean value or an error.
When a general comparison is evaluated, each operand is atomized (converted
into a sequence of atomic values). When the individual atomic values
are compared, the following rules are applied to the implicit cast
that takes place:
Atomic value in one sequence | Atomic value in other sequence | Type to which untyped value is cast |
---|---|---|
xdt:untypedAtomic | A numeric type | xs:double If you are working with very large integers, it is possible that precision might be lost. For example, when the 19 digit number −9223372036854775672 is cast to xs:double, the result is −9.223372036854776E18 (three digits of precision are lost). You can avoid this loss of precision by casting the value to an xs:decimal or xs:long type. |
xdt:untypedAtomic | xdt:untypedAtomic or xs:string | xs:string |
xdt:untypedAtomic | A value other than a numeric type, xdt:untypedAtomic, or xs:string | The type of the other value |
If the types are successfully
cast, the atomic values are compared using one of the value comparison
operators eq, ne, lt, le, gt,
or ge. The result of the comparison is true if
there is a pair of atomic values, one in the first operand sequence
and the other in the second operand sequence, for which the comparison
is true. For example, the comparison (1, 2) = (2, 3) returns
true because 2 eq 2 is true. If the implicit cast
operation fails, the comparison returns false. For example, the comparison,
[b < 3.4] in the following statement returns false because the
string "N/A" cannot be successfully cast to xs:double:
Xquery let $doc := <a><b>N/A</b></a> return $doc[b < 3.4];
Tip: To compare two sequences on an item-by-item basis, use
the XQuery function fn:deep-equal.
Examples
- The following comparison is true if the typed value of some author
subelement of $book1 is "Kennedy" as an instance
of xs:string or xdt:untypedAtomic:
$book1/author = "Kennedy"
- The following example contains three general comparisons. The
value of the first two comparisons is true, and the value of the third
comparison is false. This example illustrates the fact that general
comparisons are not transitive:
(1, 2) = (2, 3) (2, 3) = (3, 4) (1, 2) = (3, 4)
- The following example contains two general comparisons, both of
which are true. This example illustrates the fact that the = and !=
operators are not inverses of each other.
(1, 2) = (2, 3) (1, 2) != (2, 3)
- In the following example, the variables $a, $b, and $c are bound
to element nodes that have the type annotation xdt:untypedAtomic.
The first element node contains the string value "1", the second element
"2", and the third element "2.0" . In this example, the following
expression returns false because the values that are bound to $b and
$c ("2" and "2.0") are compared as strings:
However, the following expression returns true because the value that is bound to $b ("2") and the value 2.0 are compared as numbers:($a, $b) = ($c, 3.0)
($a, $b) = ($c, 2.0)