PostgreSQL Tutorial PostgreSQL Advanced PostgreSQL Database Account Management PostgreSQL References
PostgreSQL Tutorial PostgreSQL Advanced PostgreSQL Database Account Management PostgreSQL References

PostgreSQL DIV() Function



The PostgreSQL DIV() function is used for integer division where x is divided by y and an integer value is returned. In special cases it returns the following:

  • If the number y is 0, then this function returns an error.
  • If x or y or both are NULL, then this function returns an error.

Syntax

DIV(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 integer division result of x divided by y.

Example 1:

The example below shows the usage of DIV() function.

SELECT DIV(12, 3);
Result: 4

SELECT DIV(14, 3);
Result: 4

SELECT DIV(10.8, 3.1);
Result: 3

SELECT DIV(10.8, -3.1);
Result: -3

SELECT DIV(-10.8, -3.1);
Result: 3

Example 2:

Consider a database table called Sample with the following records:

Dataxy
Data 1105
Data 2206
Data 3307
Data 4408
Data 5509

To perform the integer division operation, where records of column x is divided by records of column y, the following query can be used:

SELECT *, x DIV y AS DIV_Value FROM Sample;

This will produce the result as shown below:

DataxyDIV_Value
Data 11052
Data 22063
Data 33074
Data 44085
Data 55095

❮ PostgreSQL Functions