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

How to use the instructions orin the whensection case?

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 the top two lines, I want to use something like this:

when 1 or 2 then 'First'
+3
source share
2 answers

You can do it:

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

Here is the official documentation.

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

All Articles