Reputation: 109
Here is my test T-SQL.
DECLARE @TestVal INT
SET @TestVal = 1
SELECT
CASE @TestVal
WHEN (1 | 2 | 6) THEN 'First' // I would like to check 1 or 2 or 6.
WHEN 3 THEN 'Second'
WHEN 4 THEN 'Third'
ELSE 'Other'
END
The current result is 'Other'.
I would like to get the result as 'First'. How can I use (OR statement) in my T-SQL.
Best regards
Upvotes: 3
Views: 5385
Reputation: 56
Above answers are correct also you can use OR like this:
DECLARE @TestVal INT
SET @TestVal = 2
SELECT (CASE WHEN (@TestVal = 1 OR @TestVal=2 OR @TestVal=6) THEN 'First'
WHEN @TestVal = 3 THEN 'Second'
WHEN @TestVal = 4 THEN 'Third'
ELSE 'Other'
END)
Upvotes: 2
Reputation: 107586
Try this instead:
SELECT
CASE WHEN @TestVal IN (1, 2, 6) THEN 'First'
WHEN @TestVal = 3 THEN 'Second'
WHEN @TestVal = 4 THEN 'Third'
ELSE 'Other'
END
Upvotes: 2
Reputation: 1270713
Use the condition form of case
:
SELECT (CASE WHEN @TestVal IN (1, 2, 6) THEN 'First'
WHEN @TestVal = 3 THEN 'Second'
WHEN @TestVal = 4 THEN 'Third'
ELSE 'Other'
END)
Upvotes: 6