I have a table tb3 wherein id,name,sal are to be displayed using a SELECT statement and city,descrip fields need to be displayed in the same SELECT statement only when the flag is 'Y'. How do I do this using CASE statements?
id name sal city descrip flag
7 john 80000.00 Canada prog y
6 jone 90000.00 NY test y
3 san 70000.00 NY lead y
2 sam 70000.00 Cali sub n
1 sally 60000.00 Canada archi n
4 carl 70000.00 SA plain n
I need to do something like this.. I know it's wrong , but for a sample please have a look..
declare @test varchar(1)
select @test=flag from tb3
select id,name,case @test
when 'Y' then select city,descrip from tb3
when 'n' then 'inactive'
end as status from tb3
varchar(1)is pointless - it's either 0 or 1 character, but thevarpart adds at least 2 bytes overhead for that. Any string of 5 chars or less ought to bechar(x)- in your case:char(1)