Wednesday, March 6, 2019

sql - get a comma delimited string from rows

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

No comments:

Post a Comment

plot explanation - Why did Peaches' mom hang on the tree? - Movies & 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...