How to Use SAFE_DIVIDE in BigQuery
Last updated August 30, 2026 · By the SaturnSQL team
SAFE_DIVIDE(x, y) returns NULL instead of failing when y is zero, so one empty segment cannot kill a whole ratio query. Wrap it in COALESCE to show 0 (or any sentinel) instead of NULL.
SELECT
campaign,
SAFE_DIVIDE(conversions, clicks) AS conversion_rate
FROM campaign_stats;Choosing what a zero denominator means
A plain x / y errors out the moment any row has a zero denominator. SAFE_DIVIDE turns those rows into NULL, which aggregates then ignore - often exactly right, since "no clicks" is not a 0% conversion rate. If you do want a number, COALESCE it explicitly.
SELECT
campaign,
COALESCE(SAFE_DIVIDE(conversions, clicks), 0) AS conversion_rate
FROM campaign_stats;The SAFE. prefix and IEEE_DIVIDE
SAFE_DIVIDE is the best-known member of a family: prefixing many scalar functions with SAFE. (SAFE.LOG, SAFE.PARSE_DATE, ...) makes them return NULL instead of raising an error. IEEE_DIVIDE is different - it does float division and returns Infinity or NaN for zero denominators, which is occasionally what numeric pipelines want, but NULL is usually easier to handle downstream.
SELECT
SAFE_DIVIDE(10, 0) AS safe_result, -- NULL
IEEE_DIVIDE(10, 0) AS ieee_result; -- InfinityThe portable idiom
On engines without SAFE_DIVIDE the same protection is spelled x / NULLIF(y, 0), which also works in BigQuery and is worth recognizing in queries you migrate.
SELECT conversions / NULLIF(clicks, 0) AS conversion_rate
FROM campaign_stats;Run this in SaturnSQL
SaturnSQL is a browser-based SQL editor for teams: shared query library, schema-aware autocomplete, and scheduled exports to Google Sheets and Slack.
Try it free