get a comma delimited string from rows [duplicate]

Use STUFF and FOR XML:

Create and populate sample table (Please save us this step in your future questions)

DECLARE @T AS TABLE
(
    Name varchar(10)
)

INSERT INTO @T VALUES
('John'),
('Vicky'),
('Sham'),
('Anjli'),
('Manish')

The query:

SELECT STUFF((
    SELECT ',' + Name
    FROM @T
    FOR XML PATH('')
), 1, 1, '') As [output];

Results:

output
John,Vicky,Sham,Anjli,Manish

Leave a Comment