views:

27

answers:

2

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
A: 

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

luke
+2  A: 

One way is the XML PATH trick

SELECT
    SUBSTRING(
    (
    SELECT
        ',' + columnA
    FROM
        myTable
    FOR XML PATH ('')
    )
     , 2, 7999)
FROM
     foo
gbn
@gbn i want my result in the result pane
chandru_cp
Add a CAST then...
gbn
@gbn how to do it with CAST?
chandru_cp