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 intuiti开发者_Go百科ve if(isSoon)
and goes against the grain. Why is SQL set up like this? Is it because bit fields aren't truly boolean?
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."
精彩评论