Friday, June 29, 2018

sql server - return a string of comma delimited numbers from a sql query




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

plot explanation - Why did Peaches&#39; mom hang on the tree? - Movies &amp; TV

In the middle of the movie Ice Age: Continental Drift Peaches' mom asked Peaches to go to sleep. Then, she hung on the tree. This parti...