BigQuery · Math function
BigQuery SAFE_DIVIDE
SAFE_DIVIDE(x, y) divides x by y like the / operator but returns NULL instead of failing when y is 0 or the division overflows. It is the shortest fix for the division by zero error. The SAFE. prefix gives most other scalar functions the same NULL-instead-of-error behavior.
x / y stops the whole query with an error such as division by zero: 25 / 0 as soon as one row has a zero divisor, even for FLOAT64 values. SAFE_DIVIDE(x, y) returns NULL for that row and carries on. Return types match the / operator: INT64 divided by INT64 is FLOAT64, NUMERIC stays NUMERIC, and anything with FLOAT64 is FLOAT64. See division by zero for the other fixes.
NULL is usually the right answer for a ratio without a denominator, because AVG and other aggregates skip it. Wrap the call in IFNULL(SAFE_DIVIDE(x, y), 0) only when zero is genuinely correct. IEEE_DIVIDE is the other option: it returns Infinity, -Infinity or NaN instead of NULL.
For other functions, put SAFE. in front of the name: SAFE.LOG(0), SAFE.SQRT(-4) and SAFE.PARSE_DATE('%Y-%m-%d', '2026-02-30') return NULL instead of failing. The prefix only catches errors that the function raises because of its input values; an error while computing an argument, as in SAFE.SQRT(x / 0), still fails. It doesn't work with operators, CAST (use SAFE_CAST), EXTRACT, ERROR, or aggregate, window and user-defined functions. For arithmetic there are SAFE_ADD, SAFE_SUBTRACT, SAFE_MULTIPLY and SAFE_NEGATE, which return NULL on overflow instead of an integer overflow error.
Syntax
SAFE_DIVIDE(X, Y)
SAFE.function_name(arguments)Arguments and return value
| Argument | Description |
|---|---|
X | The dividend: INT64, NUMERIC, BIGNUMERIC or FLOAT64. |
Y | The divisor, same types. Zero returns NULL instead of an error. |
Returns: The same type as X / Y: FLOAT64 for two INT64 values, NUMERIC or BIGNUMERIC when those types are involved, FLOAT64 when either side is FLOAT64. NULL when the division would fail.
Examples
Conversion rate with zero-click campaigns
WITH campaigns AS (
SELECT 'brand_search' AS campaign, 400 AS clicks, 22 AS orders UNION ALL
SELECT 'display_retargeting', 0, 0 UNION ALL
SELECT 'newsletter', 250, 5
)
SELECT
campaign,
SAFE_DIVIDE(orders, clicks) AS conversion_rate,
IFNULL(SAFE_DIVIDE(orders, clicks), 0) AS conversion_rate_or_zero
FROM campaigns
ORDER BY campaign;| campaign | conversion_rate | conversion_rate_or_zero |
|---|---|---|
| brand_search | 0.055 | 0.055 |
| display_retargeting | NULL | 0.0 |
| newsletter | 0.02 | 0.02 |
SAFE_DIVIDE vs IEEE_DIVIDE
IEEE_DIVIDE follows floating-point rules and returns Infinity or NaN. SAFE_DIVIDE returns NULL.
SELECT
SAFE_DIVIDE(10, 0) AS safe_divide,
IEEE_DIVIDE(10, 0) AS ieee_divide,
SAFE_DIVIDE(0, 0) AS safe_zero_by_zero,
IEEE_DIVIDE(0, 0) AS ieee_zero_by_zero;| safe_divide | ieee_divide | safe_zero_by_zero | ieee_zero_by_zero |
|---|---|---|---|
| NULL | Infinity | NULL | NaN |
The SAFE. prefix on other functions
Each call fails without SAFE: LOG(0) and SQRT(-4) are out of range, February 30 isn't a date, and the sum overflows INT64.
SELECT
SAFE.LOG(0) AS log_of_zero,
SAFE.SQRT(-4) AS sqrt_of_negative,
SAFE.PARSE_DATE('%Y-%m-%d', '2026-02-30') AS invalid_date,
SAFE_ADD(9223372036854775807, 1) AS int64_overflow;| log_of_zero | sqrt_of_negative | invalid_date | int64_overflow |
|---|---|---|---|
| NULL | NULL | NULL | NULL |
Result types follow the inputs
NUMERIC division keeps 9 decimal places; two INT64 operands produce FLOAT64.
SELECT
SAFE_DIVIDE(NUMERIC '100', 3) AS numeric_result,
SAFE_DIVIDE(100, 3) AS float64_result;| numeric_result | float64_result |
|---|---|
| 33.333333333 | 33.333333333333336 |
Common mistakes
- Expecting the SAFE. prefix to work on operators.
/,+and*don't take it; use SAFE_DIVIDE, SAFE_ADD, SAFE_SUBTRACT, SAFE_MULTIPLY or SAFE_NEGATE. - Expecting SAFE. to protect the arguments.
SAFE.LOG(1 / 0)still fails with division by zero, because the error happens before LOG runs. - Writing
SAFE.CAST. It fails with Function not found; useSAFE_CAST(x AS type). - Turning every NULL into 0.
IFNULL(SAFE_DIVIDE(orders, clicks), 0)reports a 0% rate for campaigns with no traffic and drags down averages of rates. Keep NULL unless zero is the true answer. - Averaging per-row ratios.
AVG(SAFE_DIVIDE(orders, clicks))weights every campaign equally;SAFE_DIVIDE(SUM(orders), SUM(clicks))gives the overall rate.
FAQ
How do I avoid division by zero in BigQuery?
Use SAFE_DIVIDE(x, y) or x / NULLIF(y, 0); both return NULL when y is 0. Wrap the result in IFNULL if you need 0, or use IEEE_DIVIDE to get Infinity and NaN.
What does the SAFE. prefix do in BigQuery?
It makes a scalar function return NULL instead of raising an error caused by its input values, for example SAFE.PARSE_DATE on an invalid string. It doesn't work on operators, CAST, EXTRACT, ERROR, or aggregate, window and user-defined functions.
What is the difference between SAFE_DIVIDE and IEEE_DIVIDE?
On a zero divisor SAFE_DIVIDE returns NULL, while IEEE_DIVIDE returns Infinity, -Infinity or NaN and always returns FLOAT64. NULL is easier to handle in aggregates; Infinity and NaN carry through later arithmetic.
Related
Errors you may hit
More mathematical functions
All BigQuery mathematical functions · All functions · BigQuery error messages
