Hi all,
One of my friend have been asked in of the interview following question,
Consider you have at table Say TblEmployee with a column say EmployeeName
which contain Records as A,B,A,A,B,C,B,C,A
your task is to write a query which will change the EmployeeName A to B and B to A.
Means after the query been executed Records will be as B,A,B,B,A,C,A,C,B
.
.
.
.
.
Think what will be the query..and try to post some answers..after two day i will post the answer
update TableTest
ReplyDeleteset EmpName = 'AA'
where EmpName = 'A'
update TableTest
set EmpName = 'A'
where EmpName = 'B'
update TableTest
set EmpName = 'B'
where EmpName = 'AA'
**********very simple way************
Declare @Emp Varchar(50)
ReplyDeleteupdate TableTest
set EmpName = CASE EmpName
WHEN 'A' THEN 'B'
WHEN 'B' THEN 'A'
ELSE EmpName
END
***** second way ************
UPDATE `employee` SET EMPLOYEENAME = (
ReplyDeleteCASE
WHEN
EMPLOYEENAME = 'A'
THEN
'B'
WHEN
EMPLOYEENAME = 'B'
THEN
'A'
ELSE
'C'
END
)
Excellent Sai..
ReplyDeletesecond query given by u is awesome one.
i.e,
update TableTest
set EmpName = CASE EmpName
WHEN 'A' THEN 'B'
WHEN 'B' THEN 'A'
ELSE EmpName
END
Even your First query works,but performance wise thats poor.