1

Consider a table,

Id columnA
1  a
2  b
3  c

Select ColumnA from table gives the result as below,

columnA
   a
   b
   c

Is it possible to get

ColumnA
a,b,c
  • 1
    duplicate of [Concatenating Column Values into a Comma-Separated List](http://stackoverflow.com/questions/1048209/concatenating-column-values-into-a-comma-separated-list) and many others. http://stackoverflow.com/search?q=%5Btsql%5D+concatenate%2Bcsv – gbn May 22 '10 at 05:22

2 Answers2

1

One way is the XML PATH trick

SELECT
    SUBSTRING(
    (
    SELECT
        ',' + columnA
    FROM
        myTable
    FOR XML PATH ('')
    )
     , 2, 7999)
FROM
     foo
gbn
  • 422,506
  • 82
  • 585
  • 676
0

heres an article describing how to do it with a stored procedure which internally uses a loop to do the concatenation.

luke
  • 14,518
  • 4
  • 46
  • 57