Request a demo

BigQuery · Conditional expression

BigQuery NULLIF

NULLIF(expr, expr_to_match) returns NULL when the two values are equal and expr otherwise. Its classic use is the divide-by-zero guard x / NULLIF(y, 0), which returns NULL instead of failing. It also turns placeholders such as '' or -1 into real NULLs.

GoogleSQL for BigQuery · Checked against the official reference in November 2026

NULLIF compares two values with =. If they are equal it returns NULL; if not, or if the comparison itself is NULL, it returns the first argument. NULLIF(sessions, 0) returns NULL for zero sessions and the session count otherwise.

That makes it the standard guard against the division by zero error: orders / NULLIF(sessions, 0) returns NULL where sessions is 0, because dividing by NULL gives NULL. Wrap the result in IFNULL if you need 0 instead. In BigQuery, / on two INT64 values returns FLOAT64, so the ratio isn't truncated to an integer as it is in PostgreSQL or SQL Server. SAFE_DIVIDE does the same job in one call.

The second use is cleaning placeholders before aggregating: AVG(NULLIF(age, -1)) ignores -1 values and COUNT(NULLIF(country, '')) doesn't count empty strings. Both arguments must share a supertype, and STRING comparison is case-sensitive: NULLIF('N/A', 'n/a') returns 'N/A'.

Syntax

NULLIF(expr, expr_to_match)

Arguments and return value

ArgumentDescription
exprThe value returned when it doesn't equal expr_to_match.
expr_to_matchThe value that should become NULL. Must share a comparable supertype with expr.

Returns: The supertype of expr and expr_to_match: NULL if the two are equal, otherwise expr.

Examples

Divide without failing on zero

The second day has no sessions. NULLIF makes the divisor NULL, so the ratio is NULL, and IFNULL turns that into 0 where a number is required.

WITH daily_traffic AS (
  SELECT DATE '2026-11-20' AS day, 1840 AS sessions, 92 AS orders UNION ALL
  SELECT DATE '2026-11-21', 0, 0
)
SELECT
  day,
  orders / NULLIF(sessions, 0) AS conversion_rate,
  IFNULL(orders / NULLIF(sessions, 0), 0) AS conversion_rate_or_zero
FROM daily_traffic
ORDER BY day;
Result
dayconversion_rateconversion_rate_or_zero
2026-11-200.050.05
2026-11-21NULL0.0

Turn placeholders into NULL before aggregating

u2 stored '' and -1 for unknown values. Without NULLIF, the -1 drags the average age down to about 20.7.

WITH signups AS (
  SELECT 'u1' AS user_id, 'DE' AS country, 34 AS age UNION ALL
  SELECT 'u2', '', -1 UNION ALL
  SELECT 'u3', 'FR', 29
)
SELECT
  COUNT(NULLIF(country, '')) AS users_with_country,
  AVG(NULLIF(age, -1)) AS avg_age,
  AVG(age) AS avg_age_with_placeholder
FROM signups;
Result
users_with_countryavg_ageavg_age_with_placeholder
231.520.666666666666668

NULL and case-sensitivity edge cases

0 = NULL is NULL, not TRUE, so NULLIF returns the 0. A NULL first argument stays NULL. String comparison is case-sensitive.

SELECT
  NULLIF(0, NULL) AS zero_vs_null,
  NULLIF(CAST(NULL AS INT64), 0) AS null_vs_zero,
  NULLIF('N/A', 'n/a') AS case_sensitive;
Result
zero_vs_nullnull_vs_zerocase_sensitive
0NULLN/A

Common mistakes

  • Swapping the arguments. NULLIF(0, sessions) returns 0 or NULL, never the session count; the column goes first.
  • Expecting integer division. orders / NULLIF(sessions, 0) returns FLOAT64 (0.05), not 0 as PostgreSQL integer division would. Use DIV when you want an integer.
  • Missing blank values. NULLIF(country, '') doesn't catch ' '; trim first with NULLIF(TRIM(country), '').
  • Guarding only one divisor. In a / NULLIF(b, 0) / c the division by c can still fail; guard each divisor or use SAFE_DIVIDE.

FAQ

How do I avoid division by zero in BigQuery?

Use x / NULLIF(y, 0) or SAFE_DIVIDE(x, y); both return NULL when y is 0. Add IFNULL(..., 0) if you want 0, or use IEEE_DIVIDE to get Infinity or NaN.

What is the difference between NULLIF and SAFE_DIVIDE?

SAFE_DIVIDE returns NULL for any division error, including overflow, in one call. NULLIF only handles the zero divisor, but it is portable SQL and works with any operator or function, not only division.

What does NULLIF return if both arguments are NULL?

NULL. The first argument is NULL, and NULLIF returns it unchanged because NULL = NULL is not TRUE.

Errors you may hit

All BigQuery conditional expressions · All functions · BigQuery error messages

Observability for every BigQuery table, in about 15 minutes

Masthead detects freshness, volume and schema anomalies, monitors pipeline errors and maps column-level lineage across all your projects, from logs and metadata only.