ANSI equivalent of IS NULL

chihwah li picture chihwah li · Feb 9, 2013 · Viewed 11.8k times · Source

I am trying to find the ANSI way to write the T-SQL 'IS NULL'. (corrected, was 'IN NULL') Some posts on the internet say you can use coalesce to make it work like 'IS NULL'

The reason I like to do this: portable code. And the query must return the rows that are NULL.

So far I created this:

SELECT empid,
       firstname,
       lastname,
       country,
       coalesce(region,'unknown') AS regions ,
       city
FROM HR.Employees

The result set looks like:

empid   firstname           lastname       country  regions city
1           Sara            Davis           USA     WA      Seattle
2           Don             Funk            USA     WA      Tacoma
3           Judy            Lew             USA     WA      Kirkland 
4           Yael            Peled           USA     WA      Redmond
5           Sven            Buck            UK      unknown London
6           Paul            Suurs           UK      unknown London
7           Russell         King            UK      unknown London
8           Maria           Cameron         USA     WA      Seattle
9           Zoya            Dolgopyatova    UK      unknown London

I identified the rows that are NULL, but how do I filter them out of this set?

Answer

Gordon Linoff picture Gordon Linoff · Feb 9, 2013

Both IS NULL and COALESCE are ANSI standard and available in almost all reasonable databases. The construct that you want, I think, is:

where region IS NULL

This is standard syntax.

To have COALESCE work like IS NULL requires a value that you know is not in the data:

where coalesce(region, '<null>') <> '<null>'

However, you would need different values for dates and numbers.