BigQuery error · Data and type conversion
division by zero
A division in your query hit a zero denominator, and in BigQuery that fails the whole query instead of returning NULL. It applies to INT64, NUMERIC and FLOAT64 alike. Use SAFE_DIVIDE(x, y), which returns NULL, or guard the denominator with NULLIF(y, 0).
The error message
division by zero: <x> / 0
Causes
A ratio over rows with a zero denominator
Conversion rates, averages per user, price per unit: any a / b where b is 0 for at least one row fails the query. The message shows the values, for example division by zero: 10 / 0 or division by zero: 1.5 / 0.
Float division errors too
Unlike IEEE floating point in many languages, / on FLOAT64 doesn't return infinity in BigQuery; it raises the same error. IEEE_DIVIDE is the function that returns +inf or NaN.
MOD and DIV
MOD(x, 0) fails with division by zero: MOD(10, 0). DIV(10, 0) reports division by zero: 10 / 0.
How to fix it
1. Use SAFE_DIVIDE
Returns NULL when the denominator is 0. Wrap in IFNULL or COALESCE if a report needs 0 instead.
WITH campaigns AS (
SELECT 'spring' AS campaign, 120 AS conversions, 4000 AS clicks UNION ALL
SELECT 'paused', 0, 0
)
SELECT
campaign,
ROUND(SAFE_DIVIDE(conversions, clicks), 4) AS conversion_rate
FROM campaigns;2. Guard the denominator with NULLIF
Portable to PostgreSQL and Snowflake, and works for the / operator directly.
SELECT 10 / NULLIF(0, 0) AS result;3. Use IEEE_DIVIDE when infinity is acceptable
Returns +inf for x / 0 and NaN for 0 / 0, which some scientific calculations expect. Avoid it in reports: inf breaks sums and averages.
SELECT IEEE_DIVIDE(10, 0) AS a, IEEE_DIVIDE(0, 0) AS b;4. Find the rows that cause it
Count zero denominators to decide whether they are valid data or a bug upstream.
WITH campaigns AS (
SELECT 'spring' AS campaign, 120 AS conversions, 4000 AS clicks UNION ALL
SELECT 'paused', 0, 0
)
SELECT COUNTIF(clicks = 0) AS zero_click_rows, COUNT(*) AS total_rows
FROM campaigns;How to prevent it
- Make
SAFE_DIVIDEthe default for every ratio in dashboards and dbt models. - A query can run for minutes before reaching the bad row. On-demand, the failed run isn't billed; under capacity pricing its slot time still counts, and the scheduled rerun fails again until the expression is fixed.
FAQ
What is the difference between SAFE_DIVIDE and NULLIF?
SAFE_DIVIDE(x, y) returns NULL on any division error, including overflow. x / NULLIF(y, 0) returns NULL only for a zero denominator. Both return NULL, not 0.
Does PostgreSQL behave the same?
Yes for integers and numerics: PostgreSQL also raises division by zero. MySQL returns NULL by default, which is why queries migrated from MySQL start failing in BigQuery.
Can I use the SAFE. prefix instead?
SAFE. works on functions, not operators, so there is no SAFE./. Use SAFE_DIVIDE, or SAFE.MOD(x, 0) and SAFE.DIV(x, 0) for those functions.
Related
SQL reference
More data and type conversion
All BigQuery error messages · What 50M daily events say about BigQuery failures
