Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

If/then statement in an SQL query?

Posted on 2008-06-10
4
Medium Priority
?
1,223 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 2000 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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Windocks is an independent port of Docker's open source to Windows.   This article introduces the use of SQL Server in containers, with integrated support of SQL Server database cloning.
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties

719 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