Home >Database >Mysql Tutorial >How Can I Combine Multiple SQL Server Rows into a Comma-Separated List for HTML?

How Can I Combine Multiple SQL Server Rows into a Comma-Separated List for HTML?

Linda Hamilton
Linda HamiltonOriginal
2025-01-06 00:38:39603browse

How Can I Combine Multiple SQL Server Rows into a Comma-Separated List for HTML?

Combining Multiple SQL Server Rows into a Comma-Delimited List for HTML

Question:

Can multiple rows in a SQL Server table be combined into a single comma-delimited list for use in HTML code?

Answer:

Yes, there are multiple methods to achieve this using SQL Server 2005.

Method 1: FOR XML PATH('') with STUFF

SELECT STUFF(( SELECT ',' + X + ',' + Y
                FROM Points
              FOR
                XML PATH('')
              ), 1, 1, '') AS XYList

Method 2: STRING_AGG

SELECT STRING_AGG(X || ',' || Y, ',') AS XYList
FROM Points

Example:

Using the example table with the following data:

X Y
12 3
15 2
18 12
20 29

Result:

XYList
----------
12,3,15,2,18,12,20,29

This result can then be used in HTML code, such as an tag, to specify a list of coordinates:

<AREA SHAPE="rect" COORDS=<XYLIST>

The above is the detailed content of How Can I Combine Multiple SQL Server Rows into a Comma-Separated List for HTML?. 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