- Categories:
Numeric functions (Rounding and Truncation)
MOD¶
Returns the remainder of input expr1 divided by input expr2.
Equivalent to the modulo arithmetic operator (for example, expr1 % expr2).
Syntax¶
Arguments¶
expr1A numeric expression.
expr2A numeric expression.
Returns¶
Returns either an integer or a fixed-point decimal number.
Usage notes¶
- Both
expr1andexpr2must be numeric expressions. They aren’t required to be integers. - The returned value is the remainder from truncation-based division (rounding towards zero), not floor-based
division (rounding down). Therefore, if
expr1is negative, the returned value is negative. This behavior is different from some programming languages (such as Python), but consistent with standard SQL. For more information, see the Modulo Wikipedia page.
Examples¶
The following example shows usage of the MOD() function on both integer
and non-integer values:
Output:
The following steps show how each result is calculated. Because MOD() uses truncation-based division
(rounding the quotient toward zero), the remainder is expr1 - (TRUNC(expr1 / expr2) * expr2):
MOD(3, 2):3 / 2is1.5, which truncates to1. The remainder is3 - (1 * 2) = 1.MOD(4.5, 1.2):4.5 / 1.2is3.75, which truncates to3. The remainder is4.5 - (3 * 1.2) = 4.5 - 3.6 = 0.9.
Because MOD() uses truncation-based division, a negative expr1 produces a negative
remainder:
Output:
MOD(-3, 2): -3 / 2 is -1.5, which truncates to -1. The remainder is -3 - (-1 * 2) = -3 + 2 = -1.