Home > Database > Mysql Tutorial > How to Achieve String Aggregation in SQL Server Before 2017?

How to Achieve String Aggregation in SQL Server Before 2017?

Mary-Kate Olsen
Release: 2025-01-20 19:21:10
Original
310 people have browsed it

SQL Server 字符串聚合 (2017 年之前)

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
Copy after login

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, ''
            );
Copy after login

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!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template