Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to negate a bit column value in SQL

How do I update a table column in order to revert its value(set true if value is false and false for true! null remains null).

Please exclude solutions where one uses case when or IIF() I want something like following

UPDATE mytable SET IsEditable = !IsEditable 
like image 513
Bellash Avatar asked Jun 19 '15 11:06

Bellash


People also ask

How do you do negation in SQL?

The SQL NOT condition (sometimes called the NOT Operator) is used to negate a condition in the WHERE clause of a SELECT, INSERT, UPDATE, or DELETE statement.

How do I nullify a column in SQL?

If you've opened a table and you want to clear an existing value to NULL, click on the value, and press Ctrl + 0 .

Can a bit column be NULL?

For any situation with three possible values, the bit datatype can be utilized by leveraging NULL as a logical value. If "not answered" and "not applicable" need stored along with "yes" and "no", a char or tinyint becomes necessary. Detecting NULLs requires a special syntax in the WHERE clause: IS [NOT] NULL.

How do I toggle a bit in SQL?

A simple method to toggle a bit column (MySQL, SQL Server, and others) As you can see, the XOR operator returns “0” if the two arguments match and “1” if they don't. This makes it easy to flip the bit in a boolean column without having to first check it's existing value.


1 Answers

You can use bitwise NOT operator:

update mytable set IsEditable = ~IsEditable 
like image 50
Andrey Korneyev Avatar answered Sep 17 '22 15:09

Andrey Korneyev