SQL nullif
returns the value of the first argument unless it equals the second argument—then it returns the null
value.
SELECT NULLIF(int_expression, 0)
FROM …
The example returns the value of int_expression
unless it contains the the value zero (0
)—then it returns the SQL null
value. This particular example is useful to prevent division by zero errors.
Tutorials
Other conditional expressions:
The nullif
expression is defined in ISO/IEC 9075:2016-2 as mandatory feature F261-03.
The essence of SQL tuning in 200 pages
Buy now!
(paperback and/or PDF)
Paperback also available at Amazon.com.
Markus offers SQL training and consulting for developers working at companies of all sizes.
Learn more »