问题描述:

Can I use a case statement as follows?

CASE

WHEN (condition1 = 1 or 2)

AND condition2 = 3

THEN result = 'Result'

ELSE

NULL

END

Conditions 1 and 2 will be looking for different values, just fyi.

If not, is there a better way to write this?

Thank you!

网友答案:

Would this work?

CASE WHEN condition1 in (1, 2) AND condition2 = 3
     THEN 'Result'
     ELSE NULL
END 
AS result
网友答案:

Well it depends on the system you are using. For example if you use Oracle and PL/SQL you can check the statement here

http://www.techonthenet.com/oracle/functions/case.php

What DB are you using? And do you want the statement in SQL or in some other kind of code or stored procedure?

网友答案:

I'm not sure what you want to do with the statement. In a select statement, it would be:

SELECT (CASE WHEN (condition1 = 1 or 2) AND condition2 = 3
             THEN  'Result'
        END) as result

You don't need the else because NULL is returned by the statement automatically if none of the when conditions are met.

In a where, it would be:

WHERE (condition1 = 1 or 2) AND (condition2 = 3) AND (result = 'Result')

The else condition is equivalent to false.

相关阅读:
Top