BigQuery · Math function
BigQuery DIV
DIV(x, y) divides two integers and drops the remainder: DIV(7, 2) is 3, while 7 / 2 is 3.5. It returns INT64 for INT64 input and truncates toward zero, so DIV(-7, 2) is -3. Use it for integer division, batch numbers and unit conversions.
In BigQuery the / operator always returns FLOAT64 for INT64 operands, so 7 / 2 is 3.5. PostgreSQL, SQL Server and Redshift return 3 for the same expression, which surprises people who migrate. DIV is integer division: DIV(7, 2) returns the INT64 value 3.
DIV truncates toward zero: DIV(-7, 2) is -3, while FLOOR(-7 / 2) is -4.0. It accepts INT64, NUMERIC and BIGNUMERIC but not FLOAT64, and the result has the wider of the two input types. MOD returns the matching remainder, so DIV(x, y) * y + MOD(x, y) always equals x.
A zero divisor raises a division by zero error, and SAFE.DIV(x, 0) returns NULL instead. Dividing the smallest INT64 value by -1 fails with an integer overflow error, because the positive result doesn't fit in INT64.
Syntax
DIV(X, Y)Arguments and return value
| Argument | Description |
|---|---|
X | The dividend: INT64, NUMERIC or BIGNUMERIC. |
Y | The divisor: INT64, NUMERIC or BIGNUMERIC. Zero raises an error. |
Returns: INT64 for two INT64 values, NUMERIC if either input is NUMERIC and neither is BIGNUMERIC, BIGNUMERIC if either is BIGNUMERIC. The quotient is truncated toward zero.
Examples
Integer division vs the / operator
SELECT
7 / 2 AS slash_division,
DIV(7, 2) AS integer_division,
MOD(7, 2) AS remainder;| slash_division | integer_division | remainder |
|---|---|---|
| 3.5 | 3 | 1 |
Negative numbers and zero divisors
DIV truncates toward zero and FLOOR rounds down. SAFE.DIV returns NULL where DIV would fail.
SELECT
DIV(-7, 2) AS div_result,
FLOOR(-7 / 2) AS floor_result,
SAFE.DIV(10, 0) AS safe_div_by_zero;| div_result | floor_result | safe_div_by_zero |
|---|---|---|
| -3 | -4.0 | NULL |
Number rows into batches of 3
Subtracting 1 first makes batch 0 hold IDs 1 to 3.
WITH events AS (
SELECT event_id FROM UNNEST(GENERATE_ARRAY(1, 7)) AS event_id
)
SELECT
DIV(event_id - 1, 3) AS batch,
ARRAY_AGG(event_id ORDER BY event_id) AS event_ids
FROM events
GROUP BY batch
ORDER BY batch;| batch | event_ids |
|---|---|
| 0 | [1, 2, 3] |
| 1 | [4, 5, 6] |
| 2 | [7] |
Common mistakes
- Expecting
/to do integer division as in PostgreSQL, SQL Server or Redshift. In BigQuery7 / 2is 3.5; useDIV(7, 2). - Using DIV as floor division on negative numbers.
DIV(-7, 2)is -3; useCAST(FLOOR(x / y) AS INT64)when you need -4. - Passing FLOAT64. DIV has no FLOAT64 version and fails with No matching signature for function DIV; cast to INT64 or NUMERIC, or use
TRUNC(x / y). - Not guarding zero divisors.
DIV(x, 0)fails the whole query; useSAFE.DIV(x, y)orDIV(x, NULLIF(y, 0)).
FAQ
How do I do integer division in BigQuery?
Use DIV(x, y), which returns INT64 for INT64 input and drops the remainder toward zero. CAST(x / y AS INT64) is not the same, because CAST rounds: CAST(7 / 2 AS INT64) is 4.
Why does 5 / 2 return 2.5 in BigQuery?
The / operator returns FLOAT64 when both operands are INT64, and NUMERIC when NUMERIC is involved. It never truncates; ask for integer division explicitly with DIV.
What is the difference between DIV and SAFE_DIVIDE?
DIV is integer division that returns INT64 and fails on a zero divisor. SAFE_DIVIDE is regular division that returns FLOAT64 for INT64 input and NULL on a zero divisor. For integer division without errors, use SAFE.DIV.
Related
Errors you may hit
More mathematical functions
All BigQuery mathematical functions · All functions · BigQuery error messages
