Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL CASE [Column] WHEN IN ('case1', 'case2') THEN 'oops' END?

Is there a way to check a CASE when it's in an array:

SELECT CASE [Option] WHEN IN (1, 3, 99) THEN 'Wrong option' ELSE 'You go!' END
like image 629
Shimmy Weitzhandler Avatar asked Jan 05 '10 04:01

Shimmy Weitzhandler


2 Answers

SELECT CASE WHEN [Option] IN (1, 3, 99) THEN 'Wrong option' ELSE 'You go' END
like image 52
jspcal Avatar answered Sep 19 '22 01:09

jspcal


You can use the CASE WHEN <predicate> THEN <value> ... END option, rather than the CASE <value> WHEN <value> THEN <value> ... END option.

SELECT CASE WHEN [Option] IN (1, 3, 99) THEN 'Wrong option' ELSE 'You go!' END

... but if the values are in a table, you could just do an outer join (and

CASE WHEN t.Value IS NULL THEN 'Not in list' ELSE 'In list' END

, or

CASE WHEN EXISTS (SELECT * FROM yourTable t WHERE t.value = [Option]) THEN 'Bad' ELSE 'Ok' END

...without a join.

like image 25
Rob Farley Avatar answered Sep 21 '22 01:09

Rob Farley