Arithmetic functions
Arithmetic operators calculate numeric values. Their result and overflow behavior depend on whether the operands are legacy SQL numbers or exact native integers and decimals.
Operators
| Operator | Calculation |
|---|---|
a + b |
Addition. |
a - b |
Subtraction. |
a * b |
Multiplication. |
a / b |
Division; ordinary integer operands give an integer quotient truncated toward zero. |
a % b |
Remainder. |
-a |
Negation. |
SQL NULL operands propagate. Ordinary numeric division or remainder by zero returns NULL. Decimal division and remainder by zero instead raise INVALID_ARGUMENT.
Exact native numeric overflow raises an error. Legacy INTEGER arithmetic can promote an overflowing result to REAL. Cast explicitly when the exact numeric family matters.
abs
Returns a number's magnitude:
abs(value)SELECT abs(-5), abs(CAST('-1.20' AS Decimal(18, 2)))
FORMAT csv;5,1.20mod and div
mod(value, divisor)
div(value, divisor)mod returns a remainder; ordinary numeric inputs return REAL, while Decimal inputs use exact decimal remainder. div returns an exact decimal integral quotient and rejects a zero divisor.
SELECT 7 / 2, 7 % 2, mod(7, 3), div(7, 2)
FORMAT csv;3,1,1.0,3Decimal arithmetic
Decimal addition and subtraction use the larger operand scale, multiplication adds scales, and division retains the left scale. Excess division digits are discarded toward zero. Convert approximate operands before combining them with Decimal.
SELECT CAST('7.00' AS Decimal(18, 2)) / 2
FORMAT csv;3.50See Decimal for ranges and conversions, and mathematical functions for roots, powers and trigonometry.