Solved

If/then statement in an SQL query?

Posted on 2008-06-10
4
1,215 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 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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

791 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