0

I have a table like

create table temp_table (col1 int)

I have some data in this table like

insert into temp_table 
values(1), (2), (3), (4)

Now I want the data to be output as follows:

1,2,3,4

I have used the query:

select cast(col1 as nvarchar)+',' 
from temp_table 
for xml path('')

The problem is that the output comes in XML format and i need it in simple text/string format. I tried searching for transpose but FOR XML() was mentioned everywhere and it didn't helped me.

Any suggestions?

marc_s
  • 704,970
  • 168
  • 1,303
  • 1,425
vstandsforvinay
  • 138
  • 1
  • 11

3 Answers3

3
declare @temp nvarchar(max)
select @temp = COALESCE(@temp + ', ', '') + CAST(col1 as nvarchar) from temp_table
select @temp
Ashok Damani
  • 3,852
  • 4
  • 27
  • 48
3

And if you really don't get it with the given link :

select STUFF((
          SELECT ',' + cast(col1 as nvarchar)
          FROM temp_table
          FOR XML PATH(''), TYPE).value('.', 'NVARCHAR(MAX)'), 1, 1, '')
Raphaël Althaus
  • 58,557
  • 6
  • 89
  • 116
1

Try this Query

Select STUFF((SELECT ',' + Cast(col1 As nvarchar) FROM temp_table 
          FOR XML PATH('')), 1, 1, '') As MyColumn
Shell
  • 6,680
  • 10
  • 36
  • 70