• RP_DBA (3/12/2013)


    Don't know that I completely understood the table/data setup but hopefully this at least points you in the right direction

    Select A.Col2

    , STUFF((

    SELECT',' + CONVERT(VARCHAR,c.Col1)

    FROM@TblB B

    Inner join @TblC C on c.Col2 = b.Col2

    WHERE B.Col1 = a.Col2

    FOR XML PATH(''),TYPE).value('.','VARCHAR(MAX)'),1,1,'')

    From @TblA A

    +1

    This is the way I do concatenation when necessary to do so. I think it's much better than the old COALESCE method because it doesn't need any variables assigned to hold the string as it's built.

    But PLEASE don't store any delimited strings in the database. I have some clients who developed their db schema before I got involved and for some reason some people just love to store delimited strings. I guess initially it's easier to do that than to create new tables and add joins to their queries. But then getting the data OUT efficiently is problematic. (Thank you Jeff Moden and others who developled DelimitedSplit8K--if I didn't have that I might have to think about becoming a .NET developer! 😛 )