PostgreSQL 18 reference · Math Functions
PostgreSQL DIV() Function
Return the integer quotient of y / x, truncated toward zero, for numeric values. Integer / integer already truncates.
PostgreSQL 18
Syntax
div(y numeric, x numeric)
Parameters
| Parameter | Description |
|---|---|
y |
The dividend. |
x |
The divisor. |
Return value
A numeric with no fractional part.
Examples
SELECT div(9, 4) AS a, div(-9, 4) AS b, 9 / 4 AS int_division, 9 / 4.0 AS decimal_division;
Output
a | b | int_division | decimal_division ---+----+--------------+-------------------- 2 | -2 | 2 | 2.2500000000000000 (1 row)
Run this on your computer — see “Get Started” for setup.
SELECT stock, div(stock, 12) AS full_boxes, mod(stock, 12) AS left_over
FROM products
WHERE product_id = 1;
Output
stock | full_boxes | left_over
-------+------------+-----------
40 | 3 | 4
(1 row)Run this on your computer — see “Get Started” for setup.
Related
MOD()· Return the remainder of dividing one number by another; x % y does the same. The sign follows the dividend.TRUNC()· Truncate a number toward zero, optionally keeping some decimal places: trunc(2.789, 2) is 2.78.- SQL Aggregate Functions · tutorial