NULLIF()

All functions > CONDITIONAL > NULLIF()

Returns NULL when two values compare equal; otherwise returns the first value

Signatures

Returns: NULL when expr equals value, otherwise expr

NULLIF(expr: T, value: T) → T
sql
ParameterTypeRequiredDescription
exprTYesValue to return when it does not compare equal to the second argument
valueTYesCompared to expr using the same rules as =

Notes

  • Both arguments must be type-compatible (same widening rules as IF branches).
  • Comparison uses ordinary equality semantics: if expr = value is unknown (for example when either side is NULL), the result is expr, not NULL.
  • Typical use: expr / NULLIF(denominator, 0) to avoid division by zero when the denominator is exactly zero.

Examples

FeatureQL
SELECT
    f1 := NULLIF(0, 0), -- Equal scalars → NULL
    f2 := NULLIF(5, 0), -- Not equal → first argument
    f3 := NULLIF(1.0E0, 0.0E0), -- Floating-point safe guard
    f4 := NULLIF(NULL::BIGINT, 0) -- Unknown equality → first argument (NULL)
;
Result
f1 BIGINTf2 BIGINTf3 VARCHARf4 BIGINT
null51.0null

Last update at: 2026/05/26 17:22:09