Comparing Expressions with NULLIF()

The NULLIF() function compares two expressions and returns null if they are equal, or the first expression otherwise. NULLIF() typically is used to convert a user-defined missing, unknown, or inapplicable value to null.

Rather than use a null, some people prefer to represent a missing value with, say, the number –1 or the string 'N/A'. DBMSes have clear rules for operations that involve nulls, so it’s sometimes desirable to convert user-defined missing values to nulls. If you want to calculate the average of the values in a column, for example, you’d get the wrong answer if you had –1 values intermingled with the real, nonmissing values. Instead you can use NULLIF() to convert the –1 values to nulls, which ...

Get SQL: Visual QuickStart Guide now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.