THE SQL Server Blog Spot on the Web
Welcome to SQLblog.com - The SQL Server blog spot on the web Sign in | Join | Help
in Search

SQLBI - Marco Russo

SQLBI is a blog dedicated to building Business Intelligence solutions with SQL Server

The use of FOR XML PATH for string concatenation

I've just read this interesting article from Anith Sen that lists many different ways to concatenate several row values into a single column (i.e. one row for each category with a field containing a comma separated list of products for that category).

I admit I never thought to use the FOR XML PATH solution to get this kind of result. Not only this is a very elegant way to write the query (you formally are not using other recursive approaches based on CTE), but it seems to be also the better solution from a performance point of view. I didn't compared it with the CLR based one, but the performance seems to be so good with the FOR XML PATH solution that I think I will use that by default unless performances are very very critical - I would consider the CLR based solution only if I already have a CLR assembly deployed, but I wouldn't deploy an assembly just for this feature.


Published Wednesday, August 13, 2008 12:09 PM by Marco Russo (SQLBI)

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

 

AaronBertrand said:

I wrote about this too, around the time SQL Server 2005 was released:

http://databases.aspfaq.com/general/how-do-i-concatenate-strings-from-a-column-into-a-single-row.html

However, I still think that in many cases it is better to do this concatenation in the front end as opposed to T-SQL.

August 13, 2008 11:40 AM
 

Marco Russo (SQLBI) said:

Aaron, thank you for your link.

Generally speaking, I agree with you to avoid this kind of transformation in T-SQL. However, if you are building report with Reporting Services, this is the better solution because the alternative in SSRS would be hard to write and to maintain.

August 13, 2008 11:52 AM
 

AaronBertrand said:

Yes, true.  But I see T-SQL concatenation recommended for the general case (not a reporting services consumer), where it is better done at presentation time.  Note I said "many" and not "all" cases.  :-)

August 13, 2008 12:18 PM
 

Marco Russo (SQLBI) said:

We completely Agree :)

August 13, 2008 12:19 PM

Leave a Comment

(required) 
(optional)
(required) 
Submit

About Marco Russo (SQLBI)

Marco Russo is a consultant, writer and trainer specialized in Business Intelligence with Microsoft technologies. He runs the SQLBI.EU website, which is dedicated to distribute resources useful for BI developers, like Integration Services components, Analysis Services models, tools, technical information and so on. Marco is certified as MCT, MCDBA, MCSD.NET, MCSA, MCSE+I.

This Blog

Syndication

Powered by Community Server (Commercial Edition), by Telligent Systems
  Privacy Statement