Raymond Morphy
Raymond Morphy

Reputation: 2526

Is it possible to use `or` in `when` section of `case` statement?

How to use or in when section of case statement?

DECLARE @TestVal INT
SET @TestVal = 1
SELECT
CASE @TestVal
WHEN 1 THEN 'First'--- this line
WHEN 2 THEN 'First'--- and this line
WHEN 3 THEN 'Third'
ELSE 'Other'
END

instead of using two above lines I want to use something like this:

when 1 or 2 then 'First'

Upvotes: 3

Views: 80

Answers (2)

Mosty Mostacho
Mosty Mostacho

Reputation: 43474

You can do this:

CASE 
    WHEN @TestVal in (1, 2) THEN 'First OR Second'
    WHEN @TestVal = 3 THEN 'Third'
    ELSE 'Other'
END

Here is the official documentation.

Upvotes: 4

juergen d
juergen d

Reputation: 204854

SELECT
CASE 
WHEN @TestVal = 1 or @TestVal = 2 THEN 'First'
...

Upvotes: 1

Related Questions