Reputation: 420
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!
Upvotes: 2
Views: 15305
Reputation: 1270463
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.
Upvotes: 0
Reputation: 1421
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?
Upvotes: 0
Reputation: 13769
Would this work?
CASE WHEN condition1 in (1, 2) AND condition2 = 3
THEN 'Result'
ELSE NULL
END
AS result
Upvotes: 1