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 DB2 XPath
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.

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

DB2® XPath 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), DB2 XPath casts the value to xs:double. If the cast fails, DB2 XPath returns an error.
  3. If the types of the operands are a valid combination for the arithmetic operator, DB2 XPath 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 an xs:integer by zero).
  4. If the types of the operands are not a valid combination for the arithmetic operator, DB2 XPath 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.
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

Syntax

arithmetic expression

Read syntax diagramSkip visual syntax diagrammultiplicative-expression +multiplicative-expression-multiplicative-expression

multiplicative expression

Read syntax diagramSkip visual syntax diagram +-path_expression *dividivmod+-path_expression

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 X.* FROM T1, XMLTABLE('declare namespace pos="http://posample.org";
                            /pos:product/description[price * .0825 > 1]'
                            PASSING T1.DESCRIPTION) X;
The following query subtracts two xs:date values, which results in the xs:yearMonthDuration value P8559D:
SELECT * FROM XMLTABLE('xs:date("2005-10-10") - xs:date("1982-05-05")') X;