Swap column values in SQL

Create table as tblSwap as shown in bellow

create table tblSwap

(

       EmpID int,

       EmpName varchar(20),

       City varchar(20),

       Country varchar(20)

)

GO


and insert new records in table using bellow script

insert into tblSwap

values(1,'Ram Kumar','India','Hyd')

insert into tblSwap

values(2,'Rayudu','UAE','Dubai')

insert into tblSwap

values(3,'Mani','India','Viz')

insert into tblSwap

values(4,'Sarath','USA','Boston')


select * from tblSwap



In my results, data got inserted in wrong columns as mentioned bellow

city data in country column and county data in city column

By using bellow query we can swap data to correct columns

select * from tblSwap

GO

update tblSwap

set City=Country

,Country=City

GO

select * from tblSwap




Comments

Post a Comment

Hi User,
Thanks for visiting My Blog and please provide your valuable feedback and subscribe for more updates. Please don't post any spam content or comments.
Thank You

Popular Posts

Failed to execute the package or element. Build errors were encountered

Exception deserializing the package "The process cannot access the file because it is being used by another process."

Temporary enable and disable SSRS subscriptions