Solved

If/then statement in an SQL query?

Posted on 2008-06-10
4
1,206 Views
Last Modified: 2009-12-16
I have a simple SQL query that returns some data in the form of numbers.

i.e. I have a column marked [race] and the numbers returned in this column can be any number bwteen 1-6.

If the number is '1' in this column, I would like it to say 'Caucasian' if it is 2, it should say 'Asian' and so on.

How would I do this?

Thanks!

0
Comment
Question by:hyphenpipe
4 Comments
 
LVL 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 total points
ID: 21754301
you have 2 options:
* create a table with 2 columns : race_id and race_name, store in there the 6 rows, and your query should make a (left) join on this table.

* use a CASE statement:
  CASE [race] WHEN 1 THE 'caucasian' WHEN 2 THEN 'asian' ... ELSE 'N/A' END

as you can imagine: the first approach has the big advantage that for new values, you just enter the pair in the table and the queries will continue to work...
0
 
LVL 8

Expert Comment

by:pzozulka
ID: 21754310
Select Field1, Field2, Field3,
CASE WHEN Field1 = 1 Then 'Caucasian'
          When Field1 = 2 Then 'Asian'
End As RACE
From Table
0
 
LVL 6

Expert Comment

by:Compaq_Engineer
ID: 21754335
You can use the CASE statement for example,
CASE Race
  WHEN 1 THEN 'Caucasian'
  WHEN 2 THEN 'Asian'
  etc....
  END
You may want to read the attached weblink http://www.4guysfromrolla.com/webtech/102704-1.shtml
0
 
LVL 2

Expert Comment

by:howyue
ID: 21766087
u have 2 options:
Method 1. hardcode the race everytime u write the a query that u wan to show the name of the race
Method 2. create a race maintenance table to store the name of the race and join it whenever u wan to show the name

take this as example:
1=Europeon
2=Asian
3=African
4=American
5=Caucasian
/* Create sample table */

CREATE TABLE Race (

	Race INT

)

CREATE TABLE RaceMaintenance (

	Race INT,

	Remark VARCHAR(20)

)

/* Insert sample data */

INSERT INTO Race

SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 

INSERT INTO RaceMaintenance

SELECT 1, 'Europeon' UNION ALL SELECT 2, 'Asian' UNION ALL SELECT 3, 'African' UNION ALL SELECT 4, 'American' UNION ALL SELECT 5, 'Caucasian' 
 

/* Method 1 */

SELECT 

	CASE Race

		WHEN 1 THEN 'Europeon'

		WHEN 2 THEN 'Asian'

		WHEN 3 THEN 'African'

		WHEN 4 THEN 'American'

		WHEN 5 THEN 'Caucasian'

		ELSE 'Other' END Race

FROM Race
 

/* Method 2 */

SELECT r.Race, m.Remark

FROM Race r

LEFT JOIN RaceMaintenance m

	ON r.Race = m.Race

Open in new window

0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Let's review the features of new SQL Server 2012 (Denali CTP3). It listed as below: PERCENT_RANK(): PERCENT_RANK() function will returns the percentage value of rank of the values among its group. PERCENT_RANK() function value always in be…
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Viewers will learn how the fundamental information of how to create a table.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now