Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can't I use a bit field as a boolean expression in a SQL case statement?

I want to select something conditionally based on whether a bit field is true or false. This was the syntax that I originally tried:

CASE WHEN isSoon THEN 'Soon' ELSE 'Not so soon' END As HowSoon

This makes sense to me since what follows the "WHEN" has to be a boolean expression, which isSoon is, as it's a bit field. However, this didn't work. What I had to do in the end was:

CASE WHEN isSoon = 1 THEN 'Soon' ELSE 'Not so soon' END As HowSoon

This seems redundant to me... It's like writing if(isSoon == True) in a programming language instead of the more intuitive if(isSoon) and goes against the grain. Why is SQL set up like this? Is it because bit fields aren't truly boolean?

like image 773
froadie Avatar asked Dec 02 '10 13:12

froadie


1 Answers

Because the bit datatype is not a boolean type, it's a datatype used to optimize the bit storage.

The fact that the string "true" and "false" can be converted to a bit can be misleading, however, quoting from MSDN , a bit is "An integer data type that can take a value of 1, 0, or NULL."

like image 155
il_guru Avatar answered Oct 07 '22 04:10

il_guru