How do I switch column values from 0 to 1 and vice versa using the update statement?
sql, sql-server, sql-server-2005, t-sql
Solution
Script 1: Demo at SQL Fiddle
UPDATE dbo.Table1 SET col2 = (CASE col2 WHEN 1 THEN 0 ELSE 1 END);
Script 2: If the values are always 0 or 1, you could use the Bitwise Exclusive OR operator. Demo at SQL Fiddle
UPDATE dbo.Table1 SET col2 = (col2 ^ 1);
Problem
I have a table `Table1` as follows ``` col1 col2 ---- ---- A 1 B 1 C 1 D 0 E 0 F 0 ``` I want the result table should be as follows (by using `Update` statement) ``` col1 col2 ---- ---- A 0 B 0 C 0 D 1 E 1 F 1 ```