sqlsql-serversql-server-2005

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

Solution

  • 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