I need to get the result like this:
I have tried using this query:
SELECT
STUFF((SELECT ', ' + CONVERT(VARCHAR(50), RuleNumber)
FROM #tempSelectPlusReferralsExtracts v2
WHERE v2.RuleApprovedDate IN
(CASE WHEN (v2.RuleApprovedDate IS NULL )
THEN NULL
ELSE v2.RuleApprovedDate
END
)
FOR XML PATH('')), 1, 2, '') [Rules],
*
FROM
#tempSelectPlusReferralsExtracts
My result looks like this:


As I mentioned, seems your
CASEexpression should be around your string aggregation, not within it: