How do I flip a bit in SQL Server?

血红的双手。 提交于 2019-12-02 17:48:50

Yes, the ~ operator will work.

update foo
set Sync = ~@IsNew

Bitwise NOT: ~

Bitwise AND: &

Bitwise OR: |

Bitwise XOR: ^

Blorgbeard

Lacking on 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.

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

~ operator will work only with BIT,

try: ~ CAST(@IsNew AS BIT)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!