Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Write a query to swap values in a table in SQL Server?

Tags:

sql

sql-server

I have mistakenly inserted wrong data in table rows, now I want to swap the data. Male in place of Female and vice-versa.

enter image description here

Following is the correct data I am expecting -

enter image description here

like image 903
Raveendra Bj Avatar asked Dec 24 '22 09:12

Raveendra Bj


1 Answers

Simple update works:

UPDATE myTable
SET 
col1 = CASE WHEN col1 = 'male' THEN 'female' ELSE 'male' END,
col2 = CASE WHEN col2 = 'female' THEN 'male' ELSE 'female' END

Result: row values will be swap.

I hope, It will work for you.

like image 193
Piyush Gupta Avatar answered Dec 27 '22 04:12

Piyush Gupta