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
Mary-Kate OlsenOriginal
2025-01-20 19:21:10327browse

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:

<code class="language-sql">select 
    string_agg(t.id,',') AS id
from 
    Table t</code>

Here's how you can tailor this query to your environment:

<code class="language-sql">select stuff( (select ',' + cast(t.id as varchar(max))
               from tabel t
               for xml path ('')
              ), 1, 1, ''
            );</code>

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:
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