Purpose
You can call this function to return the remainder of x divided by y.
Notice
Both
REMAINDER(x,y)andMOD(x,y)calculate the remainder by using the following formula:result = x - y * (x/y). The difference lies in the calculation ofx/y.REMAINDER(x,y)usesROUND(x/y), whereasMOD(x,y)usesFLOOR(x/y).
In the REMAINDER function, when the fractional part of x/y in ROUND(x/y) is exactly 0.5, if the integer part of x/y is an even number, ROUND(x/y) round the value down to the nearest integer, and if the integer part of x/y is an odd number, it rounds the value up to the nearest integer. For example, ROUND(1.5) = 2, ROUND(2.5) = 2, ROUND(3.5) = 4, ROUND(4.5) = 4.
Syntax
REMAINDER(x,y)
Parameters
| Parameter | Description |
|---|---|
| x | The dividend. It can be of any numeric data type such as NUMBER, FLOAT, BINARY_FLOAT, or BINARY_DOUBLE. |
| y | The divisor. It can be an expression, or a value of any numeric data type such as NUMBER, FLOAT, BINARY_FLOAT, or BINARY_DOUBLE. Notice y cannot be 0. |
Return type
The return type is the same as the data type of the parameter with the higher numeric precedence.
Examples
The following example uses the MOD and REMAINDER functions to return the remainder of 1.5 divided by 1.
obclient> SELECT MOD(1.5,1),REMAINDER(1.5,1) FROM DUAL;
+------------+------------------+
MOD(1.5,1) REMAINDER(1.5,1)
+------------+------------------+
.5 -.5
+------------+------------------+
1 row in set