PostgreSQL mod() Function

The PostgreSQL mod() function returns the remainder after dividing the specified two numbers.

mod() Syntax

This is the syntax of the PostgreSQL mod() function:

mod(y, x) -> numeric_type

Parameters

y

Required. The dividend. It can be data of type smallint, integer, bigint , and numeric.

x

Required. The divisor. It can be data of type smallint, integer, bigint , and numeric.

Return value

The PostgreSQL mod() function returns the remainder of y divided by x, that is the remainder of y/x.

The mod() function will return NULL if the argument is NULL.

PostgreSQL will give an error if you supply a parameter that is not a numeric type.

mod() Examples

Here are a few examples of the mod() function:

SELECT
    mod(3, 2) AS "mod(3, 2)",
    mod(3.3, 1.2) AS "mod(3.3, 1.2)";
 mod(3, 2) | mod(3.3, 1.2)
-----------+---------------
         1 |           0.9

Here,

  • 3 / 2 = 1 ... 1, so mod(3, 2) returned 1.
  • 3.3 / 1.2 = 2 ... 0.9, so mod(3.3, 1.2) returned 0.9.