How to make multiple columns a string in SQL
Following a single column to string by @Alex Aza:
select stuff(
(select ',' + cast([colum_name] as varchar)
from [dbtest].[dbo].[table]
for xml path('')),
1, 1, '')
How to use that to get all columns in a table a string?
I was thinking using dynamic SQL….
So for example
col1 col2 col3
---------------
1 7 13
2 8 14
3 9 15
4 10 16
5 11 17
6 12 18
(this results something like: 1,2,3,4,5,6 )
How to make it Return:
1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18
May be, you can try something like this.
With sample data in the table like this…
query output would be as following: