0

How can I return a comma delimited string using SQL Server?

select ID, 
(<<SomeQuery from TableB B where (B.Id = A.TableBId)>>)
from TableA A

and have it return results like:

1, '11, 12'
2, '22, 33'
0

1 Answer 1

3

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 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.