September 12, 2008 at 8:35 am
I cannot seem to get any variant of the following to work, as either a simple or searched CASE. Is it possible?
CASE Lunch
WHEN ('Lasagna' OR 'Sausage') THEN 'High'
WHEN ('Salad' OR 'Non-Fat Yoghurt') THEN 'Low'
END As FatContent
The only attempt I've gotten to work is the annoyingly redundant:
CASE
WHEN (Lunch='Lasagna' OR Lunch='Sausage') THEN 'High'
WHEN (Lunch='Salad' OR Lunch='Non-Fat Yoghurt') THEN 'Low'
END As FatContent
Thanks very much,
Rich Mechaber
September 12, 2008 at 8:51 am
rmechaber (9/12/2008)
I cannot seem to get any variant of the following to work, as either a simple or searched CASE. Is it possible?CASE Lunch
WHEN ('Lasagna' OR 'Sausage') THEN 'High'
WHEN ('Salad' OR 'Non-Fat Yoghurt') THEN 'Low'
END As FatContent
The only attempt I've gotten to work is the annoyingly redundant:
CASE
WHEN (Lunch='Lasagna' OR Lunch='Sausage') THEN 'High'
WHEN (Lunch='Salad' OR Lunch='Non-Fat Yoghurt') THEN 'Low'
END As FatContent
Thanks very much,
Rich Mechaber
That's not how CASE works in T-SQL. When you use it, there's two basic choices...
Simple cases can be done as CASE (variable or field ) WHEN (single condition) THEN (thing to do)
Complex cases (like you have) are done as CASE WHEN (complex case) THEN (thing to do)
So try this instead
CASE WHEN Lunch = 'Lasagna' OR Lunch = 'Sausage' THEN 'High'
WHEN Lunch = 'Salad' OR Lunch = 'Non-Fat Yoghurt' THEN 'Low'
END As FatContent
Or alternatively
CASE Lunch
WHEN 'Lasagna' THEN 'High'
WHEN 'Sausage' THEN 'High'
WHEN 'Salad' THEN 'Low'
WHEN 'Non-Fat Yoghurt' THEN 'Low'
END As FatContent
September 12, 2008 at 9:46 am
CASE
WHEN Lunch IN ('Lasagna','Sausage') THEN 'High'
WHEN Lunch IN ('Salad','Non-Fat Yoghurt') THEN 'Low'
WHEN Lunch IN ('Burger King') THEN 'Really High'
END As FatContent
September 12, 2008 at 9:52 am
That's it, thanks, Michael!
September 12, 2008 at 10:01 am
You probably should create a table that has the lunch type and the scale, rather than hard-coding the names into a case statement like this.
September 12, 2008 at 10:04 am
Thanks, this was a fictitious example. It's just before lunch, and I'm hungry!
Maybe I should head to Burger King?
🙂
Viewing 6 posts - 1 through 5 (of 5 total)
You must be logged in to reply to this topic. Login to reply