Solved

If/then statement in an SQL query?

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

ClickHouse in a General Analytical Workload

We have mentioned ClickHouse in some recent posts, where it showed excellent results.

In this article on Experts Exchange, we’ll look at how ClickHouse performs in a general analytical workload using the star schema benchmark test.

Question has a verified solution.

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

Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
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.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

628 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