Concatenating Row Values in Transact-SQL

A useful trick for logging subsystem based on T-SQL:

SELECT DISTINCT CategoryId, ProductNames
    FROM Northwind.dbo.Products p1
   CROSS APPLY ( SELECT
       stuff( (SELECT ','+ProductName
               FROM Northwind.dbo.Products p2
               WHERE p2.CategoryId = p1.CategoryId
               ORDER BY ProductName
               FOR XML PATH(''), TYPE).value('.', 'varchar(max)')
            ,1,1,'')
    )  D ( ProductNames )

Found here among a number of similar solutions realised various ways.

Advertisements

About fdtki

Sr. BI Developer | An accomplished, quality-driven IT professional with over 16 years of experience in design, development and implementation of business requirements as a Microsoft SQL Server 6.5-2014 | Tabular/DAX | SSAS/MDX | Certified Tableau designer
This entry was posted in programming and tagged , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s