Skip to content
Archive of posts tagged coalesce

Concatenate Rows

This solution does not require a variable and it also allows you to do joins against other tables within one statement. The substring function removes the leading comma. Download the presidents table here.

SELECT
ColumnList = SUBSTRING(
(SELECT
‘,’ + ISNULL(President,”)
FROM dbo.Presidents
FOR XML PATH(”))
,2,8000)

Using a common table expression (CTE) you can also recursively union the CTE with [...]