Concatenating Column Values into a Comma-Separated List in TSQL: A Comprehensive Guide

Dec 04, 2025 · Programming · 9 views · 7.8

Keywords: TSQL | string concatenation | comma-separated list | SQL Server

Abstract: This article explores various methods in TSQL to concatenate column values into a comma-separated string, focusing on the COALESCE-based approach for older SQL Server versions, and supplements with newer methods like STRING_AGG, providing code examples and performance considerations.

In database operations, concatenating column values from multiple rows into a single string is a common requirement, such as in report generation or data export. Using the CARS table as an example, with CarID values of 1, 2, 3 and CarName values of Porsche, Mercedes, and Ferrari, the goal is to produce a string "Porsche, Mercedes, Ferrari". Below, various implementation methods are analyzed in detail.

Core Method: Using COALESCE and Variable Assignment

In SQL Server 2016 and earlier versions, a popular approach involves variable assignment and the COALESCE function to handle string concatenation. The core idea is to use a string variable that is iteratively updated through a SELECT statement, with COALESCE managing potential NULL values. A code example is as follows:

DECLARE @result VARCHAR(MAX);
SET @result = '';
SELECT @result = 
    CASE 
        WHEN @result = '' THEN CarName
        ELSE @result + COALESCE(',' + CarName, '')
    END
FROM CARS;
PRINT @result;

In this example, we declare a VARCHAR variable @result and initialize it to an empty string. The SELECT statement retrieves CarName from the table, using a CASE expression to check if @result is empty: if so, the first value is assigned; otherwise, new values are concatenated with a comma separator using COALESCE. The COALESCE function ensures that when CarName is NULL, only an empty string is appended, avoiding extra separators. This method iterates over multiple rows efficiently but requires attention to variable scope and iterative operations.

Supplemental Methods

In addition to the above method, several other approaches are commonly used, applicable to different SQL Server versions and scenarios. First, in SQL Server 2017 and newer, the built-in function STRING_AGG can be utilized, designed specifically for string aggregation with simple syntax. For example:

SELECT STRING_AGG(CarName, ',') AS CarNames FROM CARS;

This requires only one line of code to concatenate all CarName values into a comma-separated string. STRING_AGG internally handles separators and NULL values, offering quality and performance advantages. Second, in older versions, the FOR XML PATH method is also a frequent choice. For instance:

SELECT STUFF((
    SELECT ', ' + CARS.CarName
    FROM CARS
    FOR XML PATH('')
), 1, 2, '') AS CarNames;

This method leverages FOR XML PATH to generate XML text, then uses the STUFF function to remove the leading extra separator. During parsing, it may escape HTML attribute symbols such as < and > to &lt; and &gt;, so character conversion needs to be considered. Additionally, the approach using LEFT and LEN is similar, as shown below:

SELECT LEFT(Car, LEN(Car) - 1)
FROM (
    SELECT Car + ', '
    FROM Cars
    FOR XML PATH ('')
) c (Car);

Method Comparison and Application Recommendations

When selecting a method, factors such as SQL Server version, data volume, code simplicity, and performance should be considered. For SQL Server 2017+, STRING_AGG is the optimal choice due to its built-in optimization and straightforward syntax. In older versions, the COALESCE and variable assignment method is often easier to control and debug, but attention must be paid to variable side effects. The FOR XML PATH method might lead to performance degradation with large datasets and requires caution with character escaping. Overall, it is recommended to choose the most suitable method based on the environment and to test the code thoroughly.

Conclusion

This article systematically introduces multiple methods in TSQL for concatenating column values into a comma-separated list, with COALESCE and variable assignment as the core, supplemented by STRING_AGG and FOR XML PATH. These methods have distinct advantages and are applicable to different SQL Server versions and requirements. In practical applications, it is advisable to select built-in functions or variable-based approaches to ensure efficiency and maintainability. By understanding the core principles, similar data processing challenges can be better addressed.

Copyright Notice: All rights in this article are reserved by the operators of DevGex. Reasonable sharing and citation are welcome; any reproduction, excerpting, or re-publication without prior permission is prohibited.