I wasn't getting any results where I expected them and long story short I found this odd behavior:
select 1 where 'a' in ('a', 'b', null)
-> 1
select 1 where 'a' NOT in ('b', 'c', null)
-> empty resultset
I'm relatively new to SQL Server (coming from MySQL) but this seems a bug to me, is this expected/desired behavior? I'm aware of the anything + null == null
paradigm, but if that's the explanation, then I would expect the first query to return an empty resultset just the same.
I also tried:
select 1 where NOT ('a' in ('b', 'c', null))
again an empty resultset, I'm confused now...
I added where [mycolumn] is not null
to my original query, problem solved, but I'm just curious.
Edit
I see from the duplicate marking that this depends on the setting of ansi_nulls
. When this is on, null
is treated as unknown
. And yes I can see that you cannot say with certainty that any value is absent in a collection that has an unknown value.
Why would you want to regard null
as unknown
though? They are very different concepts to me.
null
means there is no value, like asking what's in the [gum]
drawer to someone whose cabinet doesn't have a [gum]
drawer.
unknown
means there IS a value, but for some reason we don't have access to that value. Maybe the [gum]
drawer is locked, it might have content, it might be empty, but it's not null
, it's unknown
.
Where is my thinking off with this?