0

Assume this query which functions fine:

SELECT 
   first_name,
   consultant_id,
   CASE consultant_id
    WHEN 1 THEN 'First Consultant'
    WHEN 2 THEN 'Second Consultant'
   END
FROM consultant
ORDER BY first_name;

Why couldn't I add another WHEN clause using some Boolean logic other than the implied equal to in the two WHEN clauses above? For example:

SELECT 
   first_name,
   consultant_id,
   CASE consultant_id
    WHEN 1 THEN 'First Consultant'
    WHEN 2 THEN 'Second Consultant'
    WHEN BETWEEN 3 AND 12 THEN 'Everyone else'
   END
FROM consultant
ORDER BY first_name;

Which throws this error:

ORA-00936: missing expression
00936. 00000 -  "missing expression"
*Cause:    
*Action:
Error at Line: 7 Column: 10

Is there a way to make this work without using the more verbose:

SELECT 
   first_name,
   consultant_id,
   CASE 
    WHEN consultant_id = 1 THEN 'First Consultant'
    WHEN consultant_id = 2 THEN 'Second Consultant'
    WHEN consultant_id BETWEEN 3 AND 12 THEN 'Everyone else'
   END
FROM consultant
ORDER BY first_name;
4
  • 3
    You now the answer. The "more verbose" method is what you need to use. Commented Jun 5, 2018 at 13:19
  • So the WHEN clause can only handle the implied 'equal to' logic? Commented Jun 5, 2018 at 13:23
  • 2
    @ConnerM.: correct, if you need anything else you can't use the "short version" Commented Jun 5, 2018 at 13:31
  • Why the down vote? Commented Jun 5, 2018 at 13:52

2 Answers 2

2

Yes, you are right, it is unfortunately not possible to have conditions in the short syntax.

The documentation calls the short syntax "simple_case_expression"

CASE expr WHEN comparision_expr THEN return_expr
Sign up to request clarification or add additional context in comments.

Comments

2

Also, depending on your requirement, you may be able to use:

SELECT
   first_name,
   consultant_id,
   CASE consultant_id
    WHEN 1 THEN 'First Consultant'
    WHEN 2 THEN 'Second Consultant'
    ELSE 'Everyone else'
   END
FROM consultant
ORDER BY first_name
/

Comments

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.