Optimal way to concatenate/aggregate strings

前端 未结 7 1234
[愿得一人]
[愿得一人] 2020-11-22 11:17

I\'m finding a way to aggregate strings from different rows into a single row. I\'m looking to do this in many different places, so having a function to facilitate this woul

7条回答
  •  不思量自难忘°
    2020-11-22 11:57

    Update: Ms SQL Server 2017+, Azure SQL Database

    You can use: STRING_AGG.

    Usage is pretty simple for OP's request:

    SELECT id, STRING_AGG(name, ', ') AS names
    FROM some_table
    GROUP BY id
    

    Read More

    Well my old non-answer got rightfully deleted (left in-tact below), but if anyone happens to land here in the future, there is good news. They have implimented STRING_AGG() in Azure SQL Database as well. That should provide the exact functionality originally requested in this post with native and built in support. @hrobky mentioned this previously as a SQL Server 2016 feature at the time.

    --- Old Post: Not enough reputation here to reply to @hrobky directly, but STRING_AGG looks great, however it is only available in SQL Server 2016 vNext currently. Hopefully it will follow to Azure SQL Datababse soon as well..

提交回复
热议问题