Mladen Prajdić Blog

Blog about stuff and things and stuff. Mostly about SQL server and .Net

Column values Concat the SQL Server 2005 way

Here's an interesting way of contencating values in one column. It makes use of the new XML capabilities.

use master

select column_name as col1 FROM INFORMATION_SCHEMA.Columns WHERE table_name = 'spt_values'

select (select column_name as col1 FROM INFORMATION_SCHEMA.Columns WHERE table_name = 'spt_values' for xml raw, elements, type).query(' for $col1 in (row/col1) return concat($col1, ",")')

Order can be forced by using

       'for $col1 in (row/col1)
        order by $col1 ascending
        return concat($col1, ",")'

For descending order change ascending to descending.

Legacy Comments


Mladen
2006-06-07
re: Column values Concat the SQL Server 2005 way
Well this was featured on
"the WHERE clause" SQL Server podcast on SSWUG.ORG
here
http://sswug.org/sswugradio/pdetails.asp?pid=112

seems that Chuck Boyce (http://chuckboyce.blogspot.com) likes this :))

thanx Chuck!