How to flip bit fields in T-SQL?

SqlSql ServerTsql

Sql Problem Overview


I'm trying to flip a bit field in SQL Server using an update query, that is, I want to make all the 0's into 1's and vice versa. What's the most elegant solution?

There doesn't seem to be a bitwise NOT operator in T-SQL (unless I'm missing something obvious) and I haven't been able to find any other way of performing the update.

Sql Solutions


Solution 1 - Sql

You don't need a bitwise-not for this -- just XOR it with 1 / true.

To check it:

select idColumn, bitFieldY, bitFieldY ^ 1 as Toggled
from tableX

To update:

update tableX
set bitFieldY = bitFieldY ^ 1
where ...

http://msdn.microsoft.com/en-us/library/ms190277(SQL.90).aspx">MSDN T-SQL Exclusive-OR (^)

Solution 2 - Sql

Why not a simple bitfield = 1 - bitfield?

Solution 3 - Sql

Another way is

DECLARE @thebit bit = 1, @theflipbit bit

SET @theflipbit = ~ @thebit

SELECT @theflipbit

where "~" means "NOT" operator. It's clean and you get a good code to read. "negate the bit" is even cleaner and it does exactly what the "NOT" operator was designed for.

Solution 4 - Sql

I was pretty sure that most SQL flavors had a bitwise NOT, so I checked and there does appear to be one in TSQL.

From the documentation, it's the character ~.

Solution 5 - Sql

UPDATE tblTest SET MyBitField = CASE WHEN MyBitField = 1 THEN 0 ELSE 1 END

It's bland but everyone will understand what it's doing.

EDIT:

You might also need to account for nulls as suggested in the comments. Depends on your req's of course.

UPDATE tblTest SET 
   MyBitField = CASE 
      WHEN MyBitField = 1 THEN 0 
      WHEN MyBitField = 0 THEN 1
      ELSE NULL -- or 1 or 0 depending on requirements
   END

Solution 6 - Sql

A simple bitwise NOT operator (~) worked for me in SQL Server 2014 - 12.0.2269.0

In the update clause inside your T-SQL -

        Update TableName
        SET    [bitColumnName] = ~[bitColumnName],
			   ....
        WHERE  ....

Hope this helps

Ref - https://docs.microsoft.com/en-us/sql/t-sql/language-elements/bitwise-not-transact-sql

Solution 7 - Sql

Did you try this?

UPDATE mytable SET somecolumn = 
  CASE WHEN somecolumn = 0 THEN 1 
       WHEN somecolumn IS NULL THEN NULL
       WHEN somecolumn = 1 THEN 0
  END

Solution 8 - Sql

query (vb)

x = "select x from table"

update (vb)

"update table set x=" Not(x*(1))

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionBilliousView Question on Stackoverflow
Solution 1 - SqlAustin SalonenView Answer on Stackoverflow
Solution 2 - SqlgbnView Answer on Stackoverflow
Solution 3 - SqlLeonardo Marques de SouzaView Answer on Stackoverflow
Solution 4 - SqlThomas OwensView Answer on Stackoverflow
Solution 5 - SqlMayoView Answer on Stackoverflow
Solution 6 - SqlSaksham GuptaView Answer on Stackoverflow
Solution 7 - SqleKek0View Answer on Stackoverflow
Solution 8 - SqlNasa RahmanView Answer on Stackoverflow