In sql query using case when?

Posted by Karthik2010_Mca under Interview Questions on 3/26/2013 | Points: 10 | Views : 946 | Status : [Member] | Replies : 2
Hi,

In Sql server have below mentioned table.
Name Age
Karthik 26
Kumar 18
Mala 16
Keyan 28

I want output like this below,

Name Status
Karthik Adult
Kumar Major
Mala Minor
Keyan Adult

I write a query below mentioned,
select Name,
case
when Age=26 then 'Adult'
when Age=18 then 'Major'
when Age=16 then 'Minor'
when Age=28 then 'Adult'
End As [Status] From tbName

But In future I add one or more records in that table, Then How can we proceed this without alter the query.

Thanks in advance
KarthiK.K

Karthik


Responses

Posted by: Jayakumars on: 3/26/2013 [Member] [MVP] Bronze | Points: 25

Up
0
Down
hi
when will created the query First Time put ur value ELSE part


select Name,
case
when Age=26 then 'Adult'
when Age=18 then 'Major'
when Age=16 then 'Minor'
when Age=28 then 'Adult'
ELSE 'Test'
End As [Status] From tbName

Mark as Answer if its helpful to you

Karthik2010_Mca, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Mohansubramanian on: 3/26/2013 [Member] Starter | Points: 25

Up
0
Down
Hi,

if you use any condition operator. In future u have to alter it. no other way.

Regards,
Mohan
http://www.powerofmicrosoft.blogspot.in/

Regards,
Mohan Subramanian
http://mohanks.com

Karthik2010_Mca, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response