Remember != does not return nulls

Published: {ts '2020-04-07 00:00:00'}
Author: Steven Neiland
Site Url: http://www.neiland.net/article/remember-does-not-return-nulls/

I'm just trying to get back into the swing of blogging right now but I saw this mistake the other day in some sql I was migrating so I figured it was worth a note for any new developers out there.

Imagine you have a table with the following values:

Idvalue
11
220
3NULL
45

You can create this same data yourself using this script:

DECLARE @data AS TABLE (Id INT NOT NULL, value INT NULL) INSERT INTO @data ( Id, value ) VALUES (1,1) ,(2,20) ,(3,NULL) ,(4,5)

Now if I asked you to return all records where the value column is not 1 you would assume you could run this sql.

select Id, value from @data where value != 1

You would get this back, note how we are not getting back the third record:

Idvalue
220
45

The reason for this is that NULL is considered by databases as an unknown value as opposed to not existing when queried. To get around this you need to explicitly test for the NULL condition as well. So in this case the query we need is:

select Id, value from @data where value != 1 or value is null

Which returns:

Idvalue
220
3NULL
45

Its a simple mistake to make and one of many reasons why I try to avoid defining nullable columns in my databases where possible, but sometimes you don't have that option.