0

I have a table with three columns. I want to write a query which joing all the values present in the 3rd row and merge it in a single value seperated by commas.

ex -

Table A
+------+------+------+
| Col1 | Col2 | Col3 |
+------+------+------+
|    1 | A    | AB   |
|    2 | B    | BC   |
|    3 | C    | CD   |
|    4 | D    | DE   |
|    5 | E    | EF   |
|    6 | F    | FG   |
+------+------+------+

Result I want :

AB,BC,CD,DE,EF,FG

PS: I am using MS SQL server 2008

Hawk
  • 5,060
  • 12
  • 49
  • 74
user2129794
  • 2,388
  • 8
  • 33
  • 51

1 Answers1

0

You could do this with a cursor in a stored procedure. I'm sure there are far more elegant ways to concatenate strings based on a select statement return though

declare col3Cursor CURSOR FOR SELECT col3 FROM A
    open col3Cursor
    fetch next from col3Cursor into @tempVar
    while @@FETCH_STATUS = 0
    begin
returnVar = returnVar + ',' + @tempVar
end
close col3Cursor

return returnVar
Mike
  • 2,391
  • 6
  • 33
  • 72