Quantcast
Channel: Transact-SQL forum
Viewing all articles
Browse latest Browse all 23857

Nested CASE expressions in SQL Server 2008 R2

$
0
0

Hi, For some reason when I use the below CASE expression in my SELECT statement I get duplicate records.

CASE WHEN <Condition 1> THEN <Expression 1>
	 WHEN <Condition 2> THEN <Expression 2>
	 WHEN <Condition 3> THEN <Expression 3>
	 WHEN <Condition 4> THEN <Expression 4>
END AS CaseResult

Also tried using coalesce with no luck. How to write a nested CASE expression which would yeild just one expression. In case if I have to combine 2 different Expressions for 1 record, Is there a way to merge the 2 Expressions writing CASE expression something like below.

STUFF ((SELECT ',' + CASE WHEN <Condition 1> THEN <Expression 1>
	 WHEN <Condition 2> THEN <Expression 2>
	 WHEN <Condition 3> THEN <Expression 3>
	 WHEN <Condition 4> THEN <Expression 4>
     END 
     FOR XML PATH(''), ROOT('MyString'), TYPE).value('/MyString[1]','VARCHAR(MAX)'), 1, 1,'') AS CaseResult
--ResultSet

CaseResult

<Expression 1, Expression 2>  
I am using SQL Server 2008 R2. Thanks in advance.......
 

Ione


Viewing all articles
Browse latest Browse all 23857

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>