Loading ...

Swap values of column in T- SQL | CodeAsp.Net

Swap values of column in T- SQL

 /5
0 (0votes)

In this blog I will explain you how to change the value of a bit column .

Recently, I had a scenario where I had to change the value of a bit type column i.e wherever there were
1 in the column I had to replace it with 0 and vice versa. After doing some searching I found the solution.

I did it with the help of Common Table Expression (CTE).

Let's create a table and insert few records into it.

Now, lets add the record in the Common Table Expression.

As you can see in the above screen shot "0"'s are replaced with "1"s and vice versa.

There are other ways of doing it as well. But I found this easy to use and readable.

Do let me know your comments or feedbacks.

 

Comments (2)

   
mohit
Sumit you can also use this - UPDATE abc SET IsRead = (SELECT CASE WHEN IsRead ='True' THEN 'False' ELSE 'True' END)
8/11/2010
 · 
by
   
SumitJain
Ya my friend you can try this: UPDATE   TablenameSET   Tablename.ColName = (SELECT CASE WHEN Tablename.ColName = 'True' THEN 'False' ELSE 'True' END)
8/11/2010
 · 
by

Top Posts