How to use GROUP BY to concatenate strings in SQL Server?

前端 未结 20 2622
无人及你
无人及你 2020-11-21 04:33

How do I get:

id       Name       Value
1          A          4
1          B          8
2          C          9

to

id               


        
20条回答
  •  有刺的猬
    2020-11-21 04:43

    using XML path will not perfectly concatenate as you might expect... it will replace "&" with "&" and will also mess with <" and "> ...maybe a few other things, not sure...but you can try this

    I came across a workaround for this... you need to replace:

    FOR XML PATH('')
    )
    

    with:

    FOR XML PATH(''),TYPE
    ).value('(./text())[1]','VARCHAR(MAX)')
    

    ...or NVARCHAR(MAX) if thats what youre using.

    why the hell doesn't SQL have a concatenate aggregate function? this is a PITA.

提交回复
热议问题