Search This Blog

Friday, August 23, 2013

SQL SERVER: Multiple Rows into single row with comma seperated usinng STUFF

SQL SERVER: multiple rows into comma separated values using STUFF
The FOR XML PATH is the bit of magic that creates a concatenated
string from a result set.
Below is example
CREATE TABLE #tbl (c1 VARCHAR, c2 VARCHAR)
go
INSERT INTO #tbl VALUES('a',2)
INSERT INTO #tbl VALUES ('b',4)

SELECT ', ' + c1
from #tbl
for XML PATH('')

Output:
, a, b

No comments:

Post a Comment