Solved

If/then statement in an SQL query?

Posted on 2008-06-10
4
1,217 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 143

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

Major Incident Management Communications

Major incidents and IT service outages cost companies millions. Often the solution to minimizing damage is automated communication. Find out more in our Major Incident Management Communications infographic.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
EF5 How do I stop pre-compiled views? 8 54
SQL Server 2012 to SQL Server 2016 24 55
Use SSRS to email customers? 4 30
database audit for object access 6 42
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

739 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