为什么我不能在SQL case语句中使用位字段作为布尔表达式?

时间:2021-08-21 16:28:59

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:

这对我来说很有意义,因为“WHEN”必须是一个布尔表达式,因为它是一个有点字段。但是,这不起作用。我最终要做的是:

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?

这对我来说似乎是多余的......就像在编程语言中编写if(isSoon == True)而不是更直观的if(isSoon)并且反对谷物。为什么SQL设置如此?是因为位字段不是真正的布尔值吗?

1 个解决方案

#1


13  

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."

字符串“true”和“false”可以转换为位的事实可能会产生误导,但是,引用MSDN,有点是“可以取值为1,0或NULL的整数数据类型”。

#1


13  

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."

字符串“true”和“false”可以转换为位的事实可能会产生误导,但是,引用MSDN,有点是“可以取值为1,0或NULL的整数数据类型”。