Wednesday 6 March 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

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...