如何在SQL Server中翻转一点?

时间:2023-02-09 09:02:43

I'm trying to perform a bitwise NOT in SQL Server. I'd like to do something like this:

我尝试在SQL Server中执行位操作。我想做这样的事:

update foo
set Sync = NOT @IsNew

Note: I started writing this and found out the answer to my own question before I finished. I still wanted to share with the community, since this piece of documentation was lacking on MSDN (until I added it to the Community Content there, too).

注意:我开始写这篇文章,在我写完之前找到了我自己问题的答案。我仍然想与社区共享,因为MSDN上缺少这一文档(直到我将它添加到那里的社区内容)。

4 个解决方案

#1


82  

Yes, the ~ operator will work.

是的,~操作员会工作的。

update foo
set Sync = ~@IsNew

#2


23  

Bitwise NOT: ~

位不是:~

Bitwise AND: &

位和:&

Bitwise OR: |

按位或:|

Bitwise XOR: ^

位XOR:^

#3


9  

Lacking on MSDN? http://msdn.microsoft.com/en-us/library/ms173468(SQL.90).aspx

缺乏在MSDN ?http://msdn.microsoft.com/en-us/library/ms173468(SQL.90). aspx

~: Performs a bitwise logical NOT operation on an integer value. The ~ bitwise operator performs a bitwise logical NOT for the expression, taking each bit in turn. If expression has a value of 0, the bits in the result set are set to 1; otherwise, the bit in the result is cleared to a value of 0. In other words, ones are changed to zeros and zeros are changed to ones.

~:对整数值执行位逻辑非操作。按位运算符对表达式执行按位逻辑,而不是按位顺序执行。如果表达式的值为0,则结果集中的位值设置为1;否则,结果中的位值为0。换句话说,1变成了0,0变成了1。

#4


2  

For the sake of completeness:

为了完整起见:

SELECT b, 1 - b
FROM
  (SELECT cast(1 AS BIT) AS b
   UNION ALL
   SELECT cast(0 AS BIT) AS b) sampletable

#1


82  

Yes, the ~ operator will work.

是的,~操作员会工作的。

update foo
set Sync = ~@IsNew

#2


23  

Bitwise NOT: ~

位不是:~

Bitwise AND: &

位和:&

Bitwise OR: |

按位或:|

Bitwise XOR: ^

位XOR:^

#3


9  

Lacking on MSDN? http://msdn.microsoft.com/en-us/library/ms173468(SQL.90).aspx

缺乏在MSDN ?http://msdn.microsoft.com/en-us/library/ms173468(SQL.90). aspx

~: Performs a bitwise logical NOT operation on an integer value. The ~ bitwise operator performs a bitwise logical NOT for the expression, taking each bit in turn. If expression has a value of 0, the bits in the result set are set to 1; otherwise, the bit in the result is cleared to a value of 0. In other words, ones are changed to zeros and zeros are changed to ones.

~:对整数值执行位逻辑非操作。按位运算符对表达式执行按位逻辑,而不是按位顺序执行。如果表达式的值为0,则结果集中的位值设置为1;否则,结果中的位值为0。换句话说,1变成了0,0变成了1。

#4


2  

For the sake of completeness:

为了完整起见:

SELECT b, 1 - b
FROM
  (SELECT cast(1 AS BIT) AS b
   UNION ALL
   SELECT cast(0 AS BIT) AS b) sampletable