How do you flip bit field in SQL Server?
Well, if you search for this question on internet, you will find many different answers, today we will see my favorite method to flip value for the bit field.
Bit field can hold two different values zero (0) and one (1). When we change the value of zero (0) to one (1) or change the value of one (1) to zero (0) it is called as flipping the value of the bit field.
You can just flip the value of the bit field by prefixing it with ~ before it. Here is a simple script for the same.
DECLARE @field1 BIT DECLARE @field2 BIT SET @field1 = 0 SET @field2 = 1 SELECT @field1 Field1, @field2 Field2 SELECT @field1 = ~@field1, @field2 = ~@field2 SELECT @field1 Field1, @field2 Field2
You can see in above script the bit variable changes value with the help of ~ (tilda).
There are many different ways to flip the value of bit field. What is your favorite method?
Reference: Pinal Dave (https://blog.sqlauthority.com)