20

Is it possible to upgrade a bool field by telling it to update the field to the opposite of what it is without having to select the value - check it then update accordingly which seems long winded...

A pseudo example of what i mean

UPDATE `table` SET `my_bool` = opposite_of(my_bool)

Currently i have to SELECT my_bool in one query then do a quick check on its value so i can update the table in a second query.

I was hopeing to cut that down to a single query if that is possible ?

Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
Sir
  • 8,135
  • 17
  • 83
  • 146

3 Answers3

64

use NOT

UPDATE `table` SET `my_bool` = NOT my_bool
John Woo
  • 258,903
  • 69
  • 498
  • 492
28
UPDATE `Table` SET `my_bool` = 1 ^ `my_bool`
Stephen Ostermiller
  • 23,933
  • 14
  • 88
  • 109
Ishan
  • 403
  • 4
  • 9
  • 2
    This was the better option for me, for some reason I was getting errors when trying to use NOT in a INSERT INTO.....SELECT command, thanks – Starjumper Tech SL Jan 08 '20 at 10:51
1

FOR Microsoft SQL Server Kindly use ~ sign.

For example:

UPDATE [AspNetUsers] SET IsDisabled = ~IsDisabled WHERE Id = '001893bd-4e4c-4ce1-9cd1-3b155a733774'