Monday 10 April 2017

Some Tricky Ideas:

Swap values from one column to another with some Criteria's :
Most of interviewers will ask this question.Please check below example
declare @a varchar(10)
declare @b varchar(10)
set @a='Male'
set @b='Female'
select @a, case when @a='Male' then 'Female' end
select @b, case when @b='Female' then 'Male' end
Output:
@a if Male then print Female or
@b if Female then print Male
Update column A with column B values if A is Male and B is Female.
create table #tmp (A varchar(20),B varchar(20))
insert into #tmp values('Male','Female')
insert into #tmp values('Male','AA')
insert into #tmp values('Female','VV')
insert into #tmp values('Male','Female')

update #tmp set A=B , B=A 
where A='Male' and B='Female'



No comments:

Post a Comment