Home >Database >Mysql Tutorial >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!