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

Create a free account to save this page, take notes and track your progress. Sign in

Syntax

div(y numeric, x numeric)

Parameters

ParameterDescription
y The dividend.
x The divisor.

Return value

A numeric with no fractional part.

Examples

Integer division
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.

Boxes of 12
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.

Was this page helpful?