Saturday, 27 July 2013

Interview Question in sql server

Problem ::

Interchange 0 by 1 and 1 by 0 in given table ?

A most of time a interviewer asked this quetion,check below table.

Table Script with data ::

create table #temp (no_s int)

insert into #temp (no_s)values('0')     insert into #temp (no_s)values('1')
insert into #temp (no_s)values('0')     insert into #temp (no_s)values('1')
insert into #temp (no_s)values('0')     insert into #temp (no_s)values('1')
insert into #temp (no_s)values('0')     insert into #temp (no_s)values('1')
insert into #temp (no_s)values('0')     insert into #temp (no_s)values('1')
insert into #temp (no_s)values('0')     insert into #temp (no_s)values('1')
insert into #temp (no_s)values('0')     insert into #temp (no_s)values('1')
insert into #temp (no_s)values('0')     insert into #temp (no_s)values('1')

select * from #temp

Data ::


Solution ::

select ::

select no_s=case when no_s=0 then 1 else 0 end from #temp

Update ::

update #temp set no_s=case when no_s=0 then 1 else 0 end

Output ::



I hope You got something useful in this article. I would like to have feedback from my blog readers.
Your valuable feedback, question, or comments about this article are always welcome.


No comments:

Post a Comment

8451831077