SQL SERVER – How to Flip Value of Bit Field in SQL Server?

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).

SQL SERVER - How to Flip Value of Bit Field in SQL Server? flipbitfield

There are many different ways to flip the value of bit field. What is your favorite method?

Reference: Pinal Dave (https://blog.sqlauthority.com)

Previous Post
SQL SERVER – Database Stuck in Restoring State
Next Post
SQL SERVER – How to Flip Value of Bit Field in SQL Server? – Part 2

Related Posts

1 Comment. Leave new

  • 1 . if 100173953 status Pending to P
    rocessing it should not be moved more than 10 minutes
    2. if 100173953 status Processing to Out delivery it should not be moved more than 15 minutes
    3. and 100173953 status more than 120 minutes

    above conditions true means I need that increment_id’s for the below table

    Reply

Leave a Reply

Menu