December 29, 2006 at 10:26 am
I'm trying to evaluate several columns and have the answer populate a single column:
case when col1 = 1 THEN 'yes' else (when col2 = 1 THEN 'no') end as column3
I cannot get this to work and presume it's logic rather than sytax.
Thanks in advance.......................
December 29, 2006 at 10:41 am
This doesn't match the syntax from BOL, for example:
CASE ProductLine
WHEN 'R' THEN 'Road'
WHEN 'M' THEN 'Mountain'
WHEN 'T' THEN 'Touring'
WHEN 'S' THEN 'Other sale items'
ELSE 'Not for sale'
END,
So, maybe you could state the rules in English and we could help structure this. Are col1 and col2 mutually exclusive? Could you have a case where clo1 = 1 and col2 = 1? If so, what should the value then become? You might be able to do something like:
case col1
when 1 then 'yes'
else case col2
when1 then 'no'
end
end
What do you want to have happen when neither condition is true (or is that possible)?
December 29, 2006 at 10:43 am
December 29, 2006 at 10:48 am
Thanks mrpolecat I was simply missing the correct number of 'end' statements..............
December 29, 2006 at 10:52 am
January 3, 2007 at 5:18 am
Actually, there is one CASE too much in your SQL (probably just a typo):
CASE WHEN col1 = 1 THEN 'yes'
case WHEN col2 = 1 THEN 'no'
ELSE 'maybe'
END
March 24, 2007 at 8:15 am
Jeff,
You have to try like this
case when col1 = 1 THEN 'yes' else (case when col2 = 1 THEN 'no' end) end as column3
Regards
Kiruba Sankar.S
Viewing 7 posts - 1 through 6 (of 6 total)
You must be logged in to reply to this topic. Login to reply