T-SQL 中的布尔值“NOT”不适用于“位”数据类型?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/177762/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Boolean 'NOT' in T-SQL not working on 'bit' datatype?
提问by Joannes Vermorel
Trying to perform a single boolean NOT operation, it appears that under MS SQL Server 2005, the following block does not work
尝试执行单个布尔非操作,似乎在 MS SQL Server 2005 下,以下块不起作用
DECLARE @MyBoolean bit;
SET @MyBoolean = 0;
SET @MyBoolean = NOT @MyBoolean;
SELECT @MyBoolean;
Instead, I am getting more successful with
相反,我越来越成功
DECLARE @MyBoolean bit;
SET @MyBoolean = 0;
SET @MyBoolean = 1 - @MyBoolean;
SELECT @MyBoolean;
Yet, this looks a bit a twisted way to express something as simple as a negation.
然而,这看起来有点像否定这样简单的表达方式。
Am I missing something?
我错过了什么吗?
回答by Jonas Lincoln
Use the ~ operator:
使用 ~ 运算符:
DECLARE @MyBoolean bit
SET @MyBoolean = 0
SET @MyBoolean = ~@MyBoolean
SELECT @MyBoolean
回答by Galwegian
Your solution is a good one... you can also use this syntax to toggle a bit in SQL...
您的解决方案是一个很好的解决方案...您也可以使用此语法在 SQL 中进行一些切换...
DECLARE @MyBoolean bit;
SET @MyBoolean = 0;
SET @MyBoolean = @MyBoolean ^ 1;
SELECT @MyBoolean;
回答by Matt Hamilton
Subtracting the value from 1 looks like it'll do the trick, but in terms of expressing intent I think I'd prefer to go with:
从 1 中减去该值似乎可以解决问题,但就表达意图而言,我想我更愿意使用:
SET @MyBoolean = CASE @MyBoolean WHEN 0 THEN 1 ELSE 0 END
It's more verbose but I think it's a little easier to understand.
它更冗长,但我认为它更容易理解。
回答by FistOfFury
To assign an inverted bit, you'll need to use the bitwise NOT operator. When using the bitwise NOT operator, '~', you have to make sure your column or variable is declared as a bit.
要分配反转位,您需要使用按位非运算符。使用按位非运算符“~”时,必须确保将列或变量声明为位。
This won't give you zero:
这不会给你零:
Select ~1
This will:
这会:
select ~convert(bit, 1)
So will this:
这也会:
declare @t bit
set @t=1
select ~@t
回答by Keith
In SQL 2005 there isn't a real boolean value, the bit value is something else really.
在 SQL 2005 中没有真正的布尔值,位值实际上是别的东西。
A bit can have three states, 1, 0 and null (because it's data). SQL doesn't automatically convert these to true or false (although, confusingly SQL enterprise manager will)
一个位可以有三种状态,1、0 和 null(因为它是数据)。SQL 不会自动将这些转换为 true 或 false(尽管令人困惑的是 SQL 企业管理器会)
The best way to think of bit fields in logic is as an integer that's 1 or 0.
将位域视为逻辑中的最佳方式是将其视为 1 或 0 的整数。
If you use logic directly on a bit field it will behave like any other value variable - i.e. the logic will be true if it has a value (any value) and false otherwise.
如果您直接在位字段上使用逻辑,它将像任何其他值变量一样运行 - 即,如果它具有值(任何值),则逻辑将为真,否则为假。
回答by aku
BIT is a numeric data type, not boolean. That's why you can't apply boolean operators to it.
SQL Server doesn't have BOOLEAN data type (not sure about SQL SERVER 2008) so you have to stick with something like @Matt Hamilton's solution.
BIT 是数字数据类型,而不是布尔值。这就是为什么您不能对其应用布尔运算符的原因。
SQL Server 没有 BOOLEAN 数据类型(不确定 SQL SERVER 2008),所以你必须坚持使用@Matt Hamilton 的解决方案。
回答by Stephen B Craver
Use ABS
to get the absolute value (-1 becomes 1)...
使用ABS
得到绝对值(-1变为1)...
DECLARE @Trend AS BIT
SET @Trend = 0
SELECT @Trend, ABS(@Trend-1)