4

Is there a condensed form of the following statement?

SELECT Name, Case StatusID WHEN 1 THEN 'Alive' WHEN 2 THEN 'Alive' WHEN 3 THEN 'Alive' WHEN 4 THEN 'Dying' ELSE 'Dead' END FROM People

for example

CASE StatusID WHEN 1,2,3 THEN 'Alive'

or

CASE StatusID WHEN 1 OR 2 OR 3 THEN 'Alive'
2

2 Answers 2

8

depending on the DB you use the following will do the trick

SELECT 
Name, 
Case WHEN StatusID  IN ( 1, 2, 3 ) THEN 'Alive' WHEN StatusID = 4 THEN 'Dying' ELSE 'Dead' END 
FROM People
Sign up to request clarification or add additional context in comments.

Comments

5

In Oracle, assuming statuid is never <= 0:

SELECT Name, CASE WHEN statusid < 4 THEN 'Alive'
                  WHEN statusid = 4 THEN 'Dying'
                  ELSE 'Dead' END AS some_alias
  FROM people

You can also use DECODE.

2 Comments

You can't use inequalities, but you can do DECODE(1, 'Alive', 2, 'Alive', 3, 'Alive', 4, 'Dying', 'Dead'). In fact, that may actually be more compact.
Sorry... didn't specify the DBMS. It's MSSQL 2008 R2, so no DECODE.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.