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
 case

Author  Topic 

kt
Yak Posting Veteran

88 Posts

Posted - 2013-10-30 : 12:05:40
Hi,

The region table is stored the values for:
regionName
============
UNITED STATES
2
INDIA
3
HONG KONG
1
4

if regionName has the value as #, then I want to replace that number with the name, else just keep the name (UNITED STATES,INDIA,HONG KONG)

select [customer],
case regionName
when 1 then 'Asia Pacific'
when 2 then 'Europe, Middle East, & Africa'
when 3 then 'North America'
when 4 then 'South America'
else regionName
END AS region
from [dbo].[region]

thanks

James K
Master Smack Fu Yak Hacker

3873 Posts

Posted - 2013-10-30 : 12:27:47
quote:
Originally posted by kt

Hi,

The region table is stored the values for:
regionName
============
UNITED STATES
2
INDIA
3
HONG KONG
1
4

if regionName has the value as #, then I want to replace that number with the name, else just keep the name (UNITED STATES,INDIA,HONG KONG)

select [customer],
case regionName
when 1 then 'Asia Pacific'
when 2 then 'Europe, Middle East, & Africa'
when 3 then 'North America'
when 4 then 'South America'
else regionName
END AS region
from [dbo].[region]

thanks

Is that query not working? If not, try this:
SELECT  [customer] ,
CASE WHEN regionName = '1' THEN 'Asia Pacific'
WHEN regionName = '2' THEN 'Europe, Middle East, & Africa'
WHEN regionName = '3' THEN 'North America'
WHEN regionName = '4' THEN 'South America'
ELSE regionName
END AS region
FROM [dbo].[region]
Go to Top of Page

Grifter
Constraint Violating Yak Guru

274 Posts

Posted - 2013-10-30 : 12:40:08
I replicated that and it appeared to work:

select case t.Region 
when '1' then 'Asia Pacific'
when '2' then 'Europe, Middle East, & Africa'
when '3' then 'North America'
when '4' then 'South America'
else t.Region
end as region
from TempRegion t



region
=============================
UNITED STATES
Europe, Middle East, & Africa
INDIA
North America
HONG KONG
Asia Pacific
South America


Go to Top of Page

kt
Yak Posting Veteran

88 Posts

Posted - 2013-10-30 : 13:40:59
worked, thanks all !!!!
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2013-10-31 : 03:00:43
If there are more number of regions, you can have a mapping table with region codes and names and join with source table to get the names

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -