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
  • 408,740
  • 77
  • 567
  • 659
0

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

luke
  • 13,932
  • 4
  • 45
  • 57