Sub-Select to Delimited List in T-SQL

Posted by Doug Lampe on Geeks with Blogs See other posts from Geeks with Blogs or by Doug Lampe
Published on Wed, 09 Feb 2011 18:06:09 GMT Indexed on 2011/02/09 23:26 UTC
Read the original article Hit count: 326

Filed under:

The following transact-SQL statement can be used with Microsoft SQL Server to create a delimited list from a sub-query.  In this case the delimiter is a comma.

SELECT Left(item,LEN(item)-1)as delimited_list

FROM (

    select  

    CAST  

    ( 

        (  

            select original_item + ','  

            from TABLE

            where condition_field = 'value'

            for xml path ('')  

        )   as varchar(max)

     ) as item

) as temp

© Geeks with Blogs or respective owner