The following script is useful in creating comma separated row values.
Here, I have created a table called
Sample which contains two columns as
Type and enter some data as follows:
Using the Code
Here, I have used
XML PATH() functions.
(SELECT STUFF(( SELECT DISTINCT'],[' + [TYPE] FROM [Sample] _
WHERE ID=S.ID FOR XML PATH('')), 1, 2, '') + ']') AS [TYPE]
FROM [Sample] AS S
GROUP BY S.ID
The result will appear as follows:
Points of Interest
SELECT DISTINCT [TYPE] FROM [Sample] FOR XML PATH('')
XML PATH() with
select statement will yield result as follows:
STUFF() will replace the first two characters ( ],) with empty string.
- May 01, 2012: Article created