Friday 29 June 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

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print &q...