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:
Data | x | y |
---|---|---|
Data 1 | 10 | 5 |
Data 2 | 20 | 6 |
Data 3 | 30 | 7 |
Data 4 | 40 | 8 |
Data 5 | 50 | 9 |
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:
Data | x | y | DIV_Value |
---|---|---|---|
Data 1 | 10 | 5 | 2 |
Data 2 | 20 | 6 | 3 |
Data 3 | 30 | 7 | 4 |
Data 4 | 40 | 8 | 5 |
Data 5 | 50 | 9 | 5 |
❮ PostgreSQL Functions