Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CASE IN statement with multiple values

Is there a way to make a CASE statement with an IN clause?

SELECT CASE c.Number IN ('1121231','31242323') THEN 1 IN ('234523','2342423') THEN 2 END AS Test FROM tblClient c 
like image 590
mameesh Avatar asked Sep 16 '11 19:09

mameesh


People also ask

Can CASE statement return multiple values?

Of course I can write the case condition multiple times, each time return one value. However, as I have many condition need to fit, say 100. It is not good to repeat case condition again and again.

Can you have multiple conditions in a CASE statement?

Multiple conditions in CASE statementYou can evaluate multiple conditions in the CASE statement.

Can CASE statement return multiple values in Oracle?

A CASE statement cannot return more than one value, it is a function working on one value.

Can WHERE clause have multiple values?

The IN operator allows you to specify multiple values in a WHERE clause. The IN operator is a shorthand for multiple OR conditions.


1 Answers

Yes. You need to use the "Searched" form rather than the "Simple" form of the CASE expression

SELECT CASE          WHEN c.Number IN ( '1121231', '31242323' ) THEN 1          WHEN c.Number IN ( '234523', '2342423' ) THEN 2        END AS Test FROM   tblClient c   
like image 135
Martin Smith Avatar answered Sep 21 '22 22:09

Martin Smith