MOD

Returns the remainder of a division operation.

Behavior Type

Immutable

Syntax

MOD( expression1, expression2 )

Parameters

expression1

A numeric data type that specifies the dividend.

expression2

A numeric data type that specifies the divisor.

Computation Rules

When computing MOD(expression1, expression2), the following rules apply:

  • If either expression1 or expression2 is the null value, then the result is the null value.
  • If expression2 is zero, then an exception condition is raised: data exception — division by zero.
  • Otherwise, the result is the unique exact numeric value R with scale 0 (zero) such that all of the following are true:

    • R has the same sign as expression2.
    • The absolute value of R is less than the absolute value of expression1.
    • expression2 = expression1 * K + R for some exact numeric value K with scale 0 (zero).

Examples

SELECT MOD(9,4);
 mod 
-----
   1
(1 row)
SELECT MOD(10,3);
 mod 
-----
   1
(1 row) 
SELECT MOD(-10,3); 
 mod 
-----
  -1
(1 row) 
SELECT MOD(-10,-3);
 mod 
-----
  -1
(1 row)  
SELECT MOD(10,-3); 
 mod 
-----
   1
(1 row)

=> SELECT MOD(6.2,0);
ERROR 3117:  Division by zero