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.
The result of an arithmetic expression is a numeric value, an empty sequence, or an error. When an arithmetic expression is evaluated, each operand is atomized (converted into an atomic value), and the following rules are applied:
  • If the atomized operand is an empty sequence, then 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 (xdt:untypedAtomic), the value is cast to xs:double. If the cast fails, an error is returned.
If the types of the operands, after evaluation, are a valid combination for the arithmetic operator, then the operator is applied to the atomized operands, and the result of this operation is an atomic value or an 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, an error is returned.
Table 2 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:float, 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:float, 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
A + B xs:date xdt:yearMonthDuration xs:date
A + B xdt:yearMonthDuration xs:date xs:date
A + B xs:date xdt:dayTimeDuration xs:date
A + B xdt:dayTimeDuration xs:date xs:date
A + B xs:time xdt:dayTimeDuration xs:time
A + B xdt:dayTimeDuration xs:time xs:time
A + B xs:dateTime xdt:yearMonthDuration xs:dateTime
A + B xdt:yearMonthDuration xs:dateTime xs:dateTime
A + B xs:dateTime xdt:dayTimeDuration xs:dateTime
A + B xdt:dayTimeDuration xs:dateTime xs:dateTime
A + B xdt:yearMonthDuration xdt:yearMonthDuration xdt:yearMonthDuration
A + B xdt:dayTimeDuration xdt:dayTimeDuration xdt:dayTimeDuration
A - B numeric numeric numeric
A - B xs:date xs:date xdt:dayTimeDuration
A - B xs:date xdt:yearMonthDuration xs:date
A - B xs:date xdt:dayTimeDuration xs:date
A - B xs:time xs:time xdt:dayTimeDuration
A - B xs:time xdt:dayTimeDuration xs:time
A - B xs:dateTime xs:dateTime xdt:dayTimeDuration
A - B xs:dateTime xdt:yearMonthDuration xs:dateTime
A - B xs:dateTime xdt:dayTimeDuration xs:dateTime
A - B xdt:yearMonthDuration xdt:yearMonthDuration xdt:yearMonthDuration
A - B xdt:dayTimeDuration xdt:dayTimeDuration xdt:dayTimeDuration
A * B numeric numeric numeric
A * B xdt:yearMonthDuration numeric xdt:yearMonthDuration
A * B numeric xdt:yearMonthDuration xdt:yearMonthDuration
A * B xdt:dayTimeDuration numeric xdt:dayTimeDuration
A * B numeric xdt:dayTimeDuration xdt:dayTimeDuration
A idiv B numeric numeric xs:integer
A div B numeric numeric numeric; but xs:decimal if both operands are xs:integer
A div B xdt:yearMonthDuration numeric xdt:yearMonthDuration
A div B xdt:dayTimeDuration numeric xdt:dayTimeDuration
A div B xdt:yearMonthDuration xdt:yearMonthDuration xs:decimal
A div B xdt:dayTimeDuration xdt:dayTimeDuration xs:decimal
A mod B numeric numeric numeric

Examples

  • In the following example, the first expression returns the xs:decimal value -1.5, and the second expression returns the xs:integer value -1:
    -3 div 2
    -3 idiv 2
    
  • In the following expression, the subtraction of two date values results in a value of type xdt:dayTimeDuration:
    $emp/hiredate - $emp/birthdate
  • The following example illustrates the difference between a subtraction operator and hyphens that are used in the variable names unit-price and unit-discount:
    $unit-price - $unit-discount