NULLIF
conditional NULL
What changes
- Input
- two expressions
- Output or change
- NULL when equal, else the first
- Return type
- Returns the same type as the first expression. NULLIF returns the first expression if the two expressions are not equal. If the expressions are equal, NULLIF returns a null value of the type of the fi
Syntax at a glance
NULLIF ( expression , expression )
Work example
Turn a sentinel value into null before a calculation such as division.
Use a typed zero check and decide how the resulting null should appear in the report.
Check your work NULLIF returns the first expression's type and can be evaluated twice when expressions are nondeterministic.
Practice SQL
Open in SQL FlowSELECT CAST(10.0 AS decimal(9,2)) / NULLIF(2, 0) AS ratio;
Open Microsoft Learn for full syntax, permissions, and details for your version.