Every language handles null equality differently and understanding this is crucial as a misunderstanding here can lead to some quite nasty unexpected results.
In some languages NULL == NULL will be true and in others it will be false, SQL has a couple of caveats around this to be aware of. Before we look at the below examples this post assumes that you are running the default option on your SQL Server instance of ANSI_NULLS = true, if you change this setting then SQL Server will consider nulls as equal. Lets look at some examples…
If you run this you can see that NULL does not equal NULL. In SQL we use IS NULL to check for a null…
What if we want to see if two possibly null fields are equal and return true if they are both NULL or their values match? We can make use of the ISNULL operator here which specifies a value to return if the input parameter is NULL for example…
The above will return hello world because the input was null. You can use this to check if possibly null fields are equal by passing in a default value to use for null types…
What if there is no default value you can use for the ISNULL check? Then we have to get a bit more creative in our predicates…
Things also get a little weird when you start adding or concatenating non nulls with nulls….
For example this string concatenation will return null
How about this integer calculation that will also return null…
Both of these are fixed by using ISNULL and specifying default values…
So NULL is always != NULL then right? Well, ummmm, kind of…..
The UNION operator in SQL will return all results from both sides except where they match, in which case it discards the duplicate result from one side, for example the following query will return 1,2 and one of the 1 results will be discarded…
So let’s try throwing some nulls into the mix…
Based on what we know (NULL != NULL), you’d probably expect this to return 3 records (1 and 2 NULLs), however a 1 and a single NULL is all that returns, the UNION operator will filter out matching NULLs and treat them as equal.
The except and intersect operators also work in this way…
You’re probably starting to see how dangerous any value that can be null is and how easy it is to get unexpected results. One of the largest causes of bugs in software is unexpected null values that have not been properly handled. Where ever you can it will reduce risk later if you just don’t allow NULLS to get into your tables or come out of your queries (This will not always be possible).