Arithmetic expressions
Arithmetic expressions perform operations that involve addition, subtraction, multiplication, division, and modulus.
The following table describes the arithmetic operators
and lists them in order of operator precedence from highest to lowest.
Unary operators have a higher precedence than binary operators unless
parentheses are used to force the evaluation of the binary operator.
Operator | Purpose | Associativity |
---|---|---|
-(unary), +(unary) | negates value of operand, maintains value of operand | right-to-left |
*, div, idiv, mod | multiplication, division, integer division, modulus | left-to-right |
+, - | addition, subtraction | left-to-right |
Note: A subtraction
operator must be preceded by whitespace if the operator could otherwise
be interpreted as part of a previous token. For example, a-b is
interpreted as a name, but a - b and a -b are
interpreted as arithmetic operations.
|
Restriction: The operand of an arithmetic operator
cannot contain an FLWOR expression.
The result of an arithmetic expression is one of the following items:
- A numeric value
- A date or time value
- A duration value
- An empty sequence
- An error
XQuery uses the following process to evaluate an arithmetic expression.
- Atomizes each operand into a sequence of atomic values.
- Uses the following rules to evaluate the operands in the arithmetic
expression:
- If the atomized operand is an empty sequence, the result of the arithmetic expression is an empty sequence.
- If the atomized operand is a sequence that contains more than one value, an error is returned.
- If the atomized operand is an untyped atomic value (xs:untypedAtomic), XQuery casts the value to xs:double. If the cast fails, XQuery returns an error.
- If the types of the operands are a valid combination for the arithmetic operator, XQuery applies the operator to the atomized values. The result of this operation is an atomic value or a dynamic error (for example, an error might result from dividing by zero).
- If the types of the operands are not a valid combination for the arithmetic operator, XQuery raises a type error.
The following table identifies valid combinations of types
for arithmetic operators. In this table, the letter A represents the
first operand in the expression, and the letter B represents the second
operand. The term numeric denotes the types xs:integer, xs:decimal,
xs:double, or any types derived from one of these types. If the result
type of an operator is listed as numeric, the result type will be
the first type in the ordered list (xs:integer, xs:decimal, xs:double)
into which all operands can be converted by subtype substitution and
type promotion.
Operator with operands | Type of operand A | Type of operand B | Result type |
---|---|---|---|
A + B | numeric | numeric | numeric |
xs:date | xs:yearMonthDuration | xs:date | |
xs:yearMonthDuration | xs:date | xs:date | |
xs:date | xs:dayTimeDuration | xs:date | |
xs:dayTimeDuration | xs:date | xs:date | |
xs:time | xs:dayTimeDuration | xs:time | |
xs:dayTimeDuration | xs:time | xs:time | |
xs:dateTime | xs:yearMonthDuration | xs:dateTime | |
xs:yearMonthDuration | xs:dateTime | xs:dateTime | |
xs:dateTime | xs:dayTimeDuration | xs:dateTime | |
xs:dayTimeDuration | xs:dateTime | xs:dateTime | |
xs:yearMonthDuration | xs:yearMonthDuration | xs:yearMonthDuration | |
xs:dayTimeDuration | xs:dayTimeDuration | xs:dayTimeDuration | |
A - B | numeric | numeric | numeric |
xs:date | xs:date | xs:dayTimeDuration | |
xs:date | xs:yearMonthDuration | xs:date | |
xs:date | xs:dayTimeDuration | xs:date | |
xs:time | xs:time | xs:dayTimeDuration | |
xs:time | xs:dayTimeDuration | xs:time | |
xs:dateTime | xs:dateTime | xs:dayTimeDuration | |
xs:dateTime | xs:yearMonthDuration | xs:dateTime | |
xs:dateTime | xs:dayTimeDuration | xs:dateTime | |
xs:yearMonthDuration | xs:yearMonthDuration | xs:yearMonthDuration | |
xs:dayTimeDuration | xs:dayTimeDuration | xs:dayTimeDuration | |
A * B | numeric | numeric | numeric |
xs:yearMonthDuration | numeric | xs:yearMonthDuration | |
numeric | xs:yearMonthDuration | xs:yearMonthDuration | |
xs:dayTimeDuration | numeric | xs:dayTimeDuration | |
numeric | xs:dayTimeDuration | xs:dayTimeDuration | |
A idiv B | numeric | numeric | xs:integer |
A div B | numeric | numeric | numeric; but xs:decimal if both operands are xs:integer |
xs:yearMonthDuration | numeric | xs:yearMonthDuration | |
xs:dayTimeDuration | numeric | xs:dayTimeDuration | |
xs:yearMonthDuration | xs:yearMonthDuration | xs:decimal | |
xs:dayTimeDuration | xs:dayTimeDuration | xs:decimal | |
A mod B | numeric | numeric | numeric |
Syntax
arithmetic expression >>-Multiplicative-expression--+------------------------------+->< +-+--Multiplicative-expression-+ '-?--Multiplicative-expression-'
multiplicative expression .------------------------. V | >>---+---+--path_expression-+--+--------------------------------------+->< +-+-+ | .------------------------. | '-?-' | V | | '-+-*----+----+---+--path_expression-+-' +-div--+ +-+-+ +-idiv-+ '-?-' '-mod--'
Examples
The following query uses an arithmetic expression to calculate the amount that buyers pay in taxes on a product, at a rate of 8.25%, and selects the description elements for which the tax is greater than one unit of currency.
SELECT XMLQUERY ('declare namespace pos="http://posample.org";
/pos:product/description[price * .0825 > 1]'
PASSING DESCRIPTION)
FROM T1
The following query subtracts two
xs:date values, which results in the xs:yearMonthDuration value P8559D:
SELECT XMLQUERY(' xs:date("2005-10-10")
- xs:date("1982-05-05")')
FROM SYSIBM.SYSDUMMY1