Perform integer division, truncating the quotient toward zero.
DIV(a, b)
## Overview Returns the integer quotient of a divided by b, truncating toward zero. DIV is the explicit integer-division primitive for dialects where the / operator may return a DECIMAL or DOUBLE even with integer operands. Use it when you want the quotient alone, with no remainder and no implicit type promotion. DIV pairs naturally with MOD: for any valid a and b, a = DIV(a, b) * b + MOD(a, b). For the non-negative remainder companion, use PMOD. ## Behavior - Accepts INTEGER, BIGINT, or DECIMAL for both arguments. - Returns a value of the promoted integer type. - Returns NULL if either argument is NULL. - Raises a division-by-zero error when b is 0. - Truncates toward zero: DIV(-10, 3) = -3, not -4. - DIV(a, 1) equals FLOOR(a) for non-negative a and equals a for exact integers. ## Numeric precision - For integer inputs, DIV is exact. - For DECIMAL inputs, DIV truncates the quotient to an integer; it does not preserve fractional digits. - Unlike MOD, DIV never returns a fractional remainder. ## Compatibility - DIV follows the PostgreSQL integer-division convention (truncation toward zero). - In SQL dialects where / between integers already truncates, DIV is a synonym for /.
| Name | Type | Description |
|---|---|---|
a | Specifies the dividend. Accepts INTEGER, BIGINT, or DECIMAL values. | |
b | Specifies the divisor. Must be non-zero; a division-by-zero error is raised if this value is 0. |
-- Basic integer division
SELECT DIV(10, 3); -- 3
-- Negative dividend truncates toward zero
SELECT DIV(-10, 3); -- -3
-- Negative divisor
SELECT DIV(10, -3); -- -3
-- Exact division
SELECT DIV(9, 3); -- 3
-- Division of zero by a non-zero value
SELECT DIV(0, 5); -- 0
-- NULL propagation
SELECT DIV(NULL, 3); -- NULL