Concat Column of multiple records

  • Hi

    I want if multiple records are returned by inner query then Docno of all records should get concatenated with comma separation.

     

    Select T1.Itemcode,T1.name ,(select docno from order where id = T0.id)

    from tbl1 T0 inner join tbl2 on T0.itemcode = t1.itemcode

    Thanks

  • Thanks for posting your issue and hopefully someone will answer soon.

    This is an automated bump to increase visibility of your question.

  • STRING_AGG() is your friend

  • STRING_AGG isn't available before SQL 2016, right?

    Select T1.Itemcode,T1.name ,
    stuff((select ', ' + docno
    from [order] o
    where o.id = T0.id
    order by docno
    for xml path(''), type).value('.', 'varchar(8000)'), 1, 2, '') as docnos
    from tbl1 T0
    inner join tbl2 on T0.itemcode = t1.itemcode

    SQL DBA,SQL Server MVP(07, 08, 09) A socialist is someone who will give you the shirt off *someone else's* back.

Viewing 4 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic. Login to reply