SQLite MOD() Function
The SQLite MOD() function returns the remainder of x divided by y. In special cases it returns the following:
- If the number y is 0, then NULL is returned.
- If x or y or both are NULL, then NULL is returned.
This function is similar to the % operator, except that it works for non-integer arguments.
Syntax
MOD(x, y)
Parameters
x |
Required. Specify the value that will be divided by y. |
y |
Required. Specify the value that will be divided into x. |
Return Value
Returns the remainder of x divided by y.
Example 1:
The example below shows the usage of MOD() function.
SELECT MOD(12, 3); Result: 0.0 SELECT MOD(14, 3); Result: 2.0 SELECT MOD(13.5, 3.1); Result: 1.1 SELECT MOD(13.5, -3.1); Result: 1.1 SELECT MOD(13, 0); Result: NULL SELECT MOD(13, NULL); Result: NULL
Example 2:
Consider a database table called Sample with the following records:
Data | x | y |
---|---|---|
Data 1 | 10 | 5 |
Data 2 | 20 | 6 |
Data 3 | 30 | 7 |
Data 4 | 40 | 8 |
Data 5 | 50 | 9 |
To calculate the remainder of division operation, where records of column x is divided by records of column y, the following query can be used:
SELECT *, MOD(x, y) AS MOD_Value FROM Sample;
This will produce the result as shown below:
Data | x | y | MOD_Value |
---|---|---|---|
Data 1 | 10 | 5 | 0.0 |
Data 2 | 20 | 6 | 2.0 |
Data 3 | 30 | 7 | 2.0 |
Data 4 | 40 | 8 | 0.0 |
Data 5 | 50 | 9 | 5.0 |
❮ SQLite Functions