-1

I have a SQL Server output which returns the following values with one column:

RepresentID 
---------------
111122222
3333344444
5555566666
000000090909
7777788888
9999999999
121212131313
141414151515

Output :

RepresentID
---------------
111122222
3333344444
5555566666
000000090909
7777788888
9999999999
121212131313
141414151515
marc_s
  • 704,970
  • 168
  • 1,303
  • 1,425
Kapil
  • 1,743
  • 6
  • 22
  • 42

1 Answers1

1

need to concatenate all the rows in to one row.

use xml path like below

 SELECT STUFF((
        SELECT
            ','+cast(representid as varchar(30))
        FROM
          table 
        FOR
            XML PATH('')
        ),1,1,''
    ) AS comma_seperated_list;

On sql2017 , you can use string_aggregate like below

select string_agg(cast(representid as varchar(40)),',')
from table 
TheGameiswar
  • 26,582
  • 7
  • 53
  • 87
  • This is a good solution, but to get a line break between each element instead of a comma-separated list, you can use replace the comma with CHAR(13) (carriage return), CHAR(10) (line feed), or CHAR(13) + CHAR(10) (CRLF). – Max Szczurek Mar 28 '18 at 04:16