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.
Table 1. Arithmetic operators in XQuery
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: Start of changeThe operand of an arithmetic operator cannot contain an FLWOR expression.End of change

The result of an arithmetic expression is one of the following items:

  • A numeric value
  • Start of changeA date or time valueEnd of change
  • Start of changeA duration valueEnd of change
  • An empty sequence
  • An error

XQuery uses the following process to evaluate an arithmetic expression.

  1. Atomizes each operand into a sequence of atomic values.
  2. 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.
  3. 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).
  4. 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.
Start of change
Table 2. Valid types for operands of arithmetic expressions
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
End of change

Syntax

Read syntax diagram
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
Start of changeThe 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
End of change