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
| Parameter | Type | Required | Description |
|---|---|---|---|
expr | T | Yes | Value to return when it does not compare equal to the second argument |
value | T | Yes | Compared to expr using the same rules as = |
Notes
- Both arguments must be type-compatible (same widening rules as
IFbranches). - Comparison uses ordinary equality semantics: if
expr = valueis unknown (for example when either side is NULL), the result isexpr, 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 BIGINT | f2 BIGINT | f3 VARCHAR | f4 BIGINT |
|---|---|---|---|
| null | 5 | 1.0 | null |
On this page