January 6, 2010 at 3:53 pm
What am I doing wrong? Grr.
select *,mycolumn=
CASE
WHEN mycolumn= 'PSI' THEN mbrproductcode
WHEN mycolumn= 'PPO' THEN 10001912
WHEN mycolumn= 'PP1' THEN 10001910
WHEN mycolumn= 'NOT' THEN 10001909
WHEN mycolumn= 'MIN' THEN 10001908
WHEN mycolumn= 'IND' THEN 10001907
WHEN mycolumn= 'EP1' THEN 10001906
ELSE 'Unknown'
END,
FROM from mytable
January 6, 2010 at 4:17 pm
ToddJames (1/6/2010)
What am I doing wrong? ...
It depends on the error message you receive.
I'd expect something like
Msg 245, Level 16, State 1, Line 1
Conversion failed when converting the varchar value 'Unknown' to data type int.
The reason is simple: the data type within a CASE statement needs to be identical or at least implicit convertable. The resulting data type will be with respect to data type precedence. In your case you have INT and VARCHAR and something else (I don't know the data type of mbrproductcode).
You have two options: Either change your integer values to character or vice versa.
January 6, 2010 at 4:27 pm
How about this? Same issue?
select *,mycolumn=
CASE
WHEN mycolumn= 'PSI' THEN 10001911
WHEN mycolumn= 'PPO' THEN 10001912
WHEN mycolumn= 'PP1' THEN 10001910
WHEN mycolumn= 'NOT' THEN 10001909
WHEN mycolumn= 'MIN' THEN 10001908
WHEN mycolumn= 'IND' THEN 10001907
WHEN mycolumn= 'EP1' THEN 10001906
ELSE 'Unknown'
January 6, 2010 at 4:30 pm
Nothing really changed except you removed the reference to your column.
Please reread my previous reply.
If it's working or not: give it a try in SSMS. But the answer will be: no.
January 6, 2010 at 11:17 pm
ToddJames (1/6/2010)
What am I doing wrong? Grr.select *,mycolumn=
CASE
WHEN mycolumn= 'PSI' THEN mbrproductcode
WHEN mycolumn= 'PPO' THEN 10001912
WHEN mycolumn= 'PP1' THEN 10001910
WHEN mycolumn= 'NOT' THEN 10001909
WHEN mycolumn= 'MIN' THEN 10001908
WHEN mycolumn= 'IND' THEN 10001907
WHEN mycolumn= 'EP1' THEN 10001906
ELSE 'Unknown'
END,
FROM from mytable
Must look eye... just remove the comma from the END keyword.
--Jeff Moden
Change is inevitable... Change for the better is not.
January 7, 2010 at 4:04 am
Jeff Moden (1/6/2010)
ToddJames (1/6/2010)
What am I doing wrong? Grr.select *,mycolumn=
CASE
WHEN mycolumn= 'PSI' THEN mbrproductcode
WHEN mycolumn= 'PPO' THEN 10001912
WHEN mycolumn= 'PP1' THEN 10001910
WHEN mycolumn= 'NOT' THEN 10001909
WHEN mycolumn= 'MIN' THEN 10001908
WHEN mycolumn= 'IND' THEN 10001907
WHEN mycolumn= 'EP1' THEN 10001906
ELSE 'Unknown'
END,
FROM from mytable
Must look eye... just remove the comma from the END keyword.
It still won't work, I guess. (have a look at the data type of the ELSE clause compared to the rest). I tried to explain it in my first reply already. Seems like the message didn't go through though...
I would expect an error message saying
Msg 245, Level 16, State 1, Line 1
Conversion failed when converting the varchar value 'Unknown' to data type int.
January 7, 2010 at 8:48 am
Ah.... must look eye myself... I missed the two different datatypes. Sorry. NULL means "Unknown" and would work here.
--Jeff Moden
Change is inevitable... Change for the better is not.
Viewing 7 posts - 1 through 6 (of 6 total)
You must be logged in to reply to this topic. Login to reply