Concatenate strings from c column into a single row is very common SQL operations in your project. However, this function is not provided directly by SQLSERVER. We should use several tricks to fulfill this feature.
There's a good article which provide how to write SQL statements for this feature[1].
Originally, there are several methods:
- 1. use UDF(user defined function) for Group by statement[1,2]. For the item which will be grouped, we can concatenate all return rows for a column to a single row using this UDF.
This method is easy to understand, but the performance is the slowest in the three methods. - 2. use " FOR XML PATH ('') " clause[1].
- 3. use " cross apply" and " For XML PATH('') " clauses[1].
This is the best one I think. It's not only easy to understand but also the performance is good.