Implementing string aggregation before SQL Server 2017
For those using SQL Server 2014 or earlier and want to concatenate strings like the example query:
select string_agg(t.id,',') AS id from Table t
Here's how you can tailor this query to your environment:
select stuff( (select ',' + cast(t.id as varchar(max)) from tabel t for xml path ('') ), 1, 1, '' );
In this query, the stuff()
function is only used to remove the leading comma. The actual string concatenation is done using for xml path
.
The above is the detailed content of How to Achieve String Aggregation in SQL Server Before 2017?. For more information, please follow other related articles on the PHP Chinese website!