NULLIF()

Use the nullif() function as shorthand for a searched case statement where two equal expressions return null.

nullif() takes the following syntax. Its two arguments are the expressions that you want to compare.

1nullif(fieldName1, fieldName2)

In this example, the query returns null for all City values that are Aberdeen. If the City value is not Aberdeen, then it returns the City value.

Here’s how to write the query as a case statement.

1SELECT CASE WHEN City='Aberdeen'
2THEN NULL
3ELSE City
4END AS City
5FROM "Superstore"
6GROUP BY City;

Here’s how to write the same query using nullif().

1SELECT NULLIF(City, 'Aberdeen') as City
2FROM "Superstore"
3GROUP BY City;
City
Aberdeen
Akron
Albuquerque
Alexandria