Given:
The following Select statement:
select case NULL
when NULL then 0
else 1
end
Problem:
I’m expecting this to return 0 but instead it returns 1. What gives?
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.
Generally speaking, NULL is not something you should attempt to compare for equality, which is what a case statement does. You can use “Is NULL” to test for it. There is no expectation that
NULL != NULLor thatNULL = NULL. It’s an indeterminate, undefined value, not a hard constant.— To encompass questions in the comments —
If you need to retrieve a value when you may encounter a NULL column, try this instead:
I believe that should work. As far as your original post is concerned:
The trouble is that your Case select automatically attempts to use equality operations. That simply doesn’t work with NULL.