How can I return a comma delimited string using SQL Server?
select ID,
(<>)
from TableA A
and have it return results like:
1, '11, 12'
2, '22, 33'
Answer
You can use STUFF(), See Demo Here
SELECT ID
,STUFF((SELECT ', ' + CAST(data AS VARCHAR(10)) [text()]
FROM B
WHERE TableBId = A.ID
FOR XML PATH(''), TYPE)
.value('.','NVARCHAR(MAX)'),1,2,' ') Comma_Output
FROM A
GROUP BY ID
No comments:
Post a Comment