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
Release: 2025-01-06 00:38:39
Original
601 people have browsed it

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

Method 2: STRING_AGG

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

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

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

<AREA SHAPE="rect" COORDS=<XYLIST>
Copy after login

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!

source:php.cn
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