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
Thanks for your information
ReplyDelete