Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Mapping int to strings

Author  Topic 

lafuel
Starting Member

1 Post

Posted - 2013-05-08 : 09:42:29
I've got some ints (1-6) that I need to convert and map to say 1a, 2b, 3c, e.g. strings.

For normal mapping to ints I'd just use

SELECT SomeInts, case when SomeInts = 1 THEN 2 when SomeInts = 2 THEN 3 ELSE 999 END AS SomeIntsNEW FROM SomeTable

But if I try to change the first THEN to '2a' I get this:

Conversion failed when converting the varchar value '2y' to data type int.

I've tried various variants over cast/convert but none worked.

Whats the right syntax?

MIK_2008
Master Smack Fu Yak Hacker

1054 Posts

Posted - 2013-05-08 : 10:31:30
declare @t table (col1 int)
insert into @t values (1),(2),(3),(4),(5),(6)
select col1
,CASE
WHEN col1=1 THEN CONVERT(varchar,col1)+'a'
WHEN col1=2 THEN CONVERT(varchar,col1)+'b'
WHEN col1=3 THEN CONVERT(varchar,col1)+'c'
WHEN col1=4 THEN CONVERT(varchar,col1)+'d'
WHEN col1=5 THEN CONVERT(varchar,col1)+'e'
WHEN col1=6 THEN CONVERT(varchar,col1)+'f'
END conversion
from @t

Cheers
MIK
Go to Top of Page
   

- Advertisement -