declare @test int
declare @test2 int = 3
select case when @test != @test2 then 1 else 0 end
select case when @test = @test2 then 1 else 0 end
The result is always 0. Can someone explain this?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
This behavior is controlled by the ANSI_NULLS set option. In fact if you SET ANSI_NULLS OFF you will get a 1 for the first select.
NULL is a special value, it is an unknown value. You can read your queries like this:
Select a value, when an unknown value is not equal to 3
Select a value, when an unknown value is equal to 3
Since the value is unknown it may or may not match, so you can’t assume that it’s not equal, as it is possible that they are equal. Since your comparing an unknown value it’s like your comparing all possible values, and since there is one possible value that invalidates it the statement is false.