'How to pass multiple values in THEN clause in CASE statement sql server
How to use multiple values in THEN clause of CASE statement in Sql Server 2008?
e.g.
Select CASE Country
WHEN 'UNITED' THEN Country In ('ABC United','ABS United','X')
WHEN 'CORE' THEN country in ('p','q','r')
So basically here my United and core are agrregated values and when selected by user , I want to pass respective values in then clause (Which are non-aggregated) Any suggestions?
Solution 1:[1]
You could use something based on:
DECLARE @Country as varchar(20)
SELECT @Country = 'United'
IF @Country = 'UNITED'
SELECT '1', '2'
IF @Country = 'CORE'
SELECT '3', '4'
I am sure there are neater ways of doing this.
Solution 2:[2]
SELECT CASE Country
WHEN 'UNITED'
THEN CASE
WHEN Country IN (
'ABC United'
,'ABS United'
,'X'
)
THEN Country
ELSE ''
END
WHEN 'CORE'
THEN CASE
WHEN country IN (
'p'
,'q'
,'r'
)
THEN country
ELSE ''
END
ELSE ''
END
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Peter Smith |
| Solution 2 |
