SQL Server 2012 and NULL comparison -
can explain me why these 2 statements returns different results?
select case when not((null = null) or (1 != 1)) 1 else 0 end select case when not((null = null) , (1 != 1)) 1 else 0 end
i know null
compared gives false , wanted use property stopped @ commands similar above. real statements instead of nulls use variables can null
simplified them show problem. thought has operation order seems that's not it.
i know null compared gives false
this isn't correct, null
compared evaluates unknown, not false, quick example:
select case when (null = null) 'true' when not(null = null) 'false' else 'other' end
will give third option of other
.
if rewrite logic (still same meaning, becomes more clear):
select case when (null <> null) , (1 = 1) 1 else 0 end select case when (null <> null) or (1 = 1) 1 else 0 end
so in first instance have when [unknown] , [true]
false, in second have when [unknown] or [true]
true, returns 1.
if rewrite query variables, inspect execution plan xml, can see sql server rewrites expression above during compilation:
declare @a int = null, @b int = null, @c int = 1, @d int = 1; select top 1 case when not((@a = @b) or (@c != @d)) 1 else 0 end, case when not((@a = @b) , (@c != @d)) 1 else 0 end
Comments
Post a Comment