SqlShare.com Logo
 
Skip Navigation Links
Home
Video List
Classes
About Us
Login / Register
Subscribe RSS Feed 

Idera Virtual Database

Row Concatenation Using FOR XML PATH

One of the most requested T-SQL statements is the ability to combine a number of records in one line concatenated together. This video shows you how to accomplish this with FOR XML PATH

Duration:
2 mins 13 secs
Skill Level:
100
Rating:
4.28 out of 5
Publish Date:
November 03, 2008
Row Concatenation Using FOR XML PATH Watch Video Now  Watch it later!
Bookmark and Share
 
1=Poor, 3=Good, 5=Excellent

About the Author

Image of Plamen Ratchev
Plamen Ratchev has over two decades experience in the software architecture and development field. He has enjoyed building solutions for UNIX, DOS and Windows platforms, with main focus on SQL Server since version 4.21. Plamen is founder of Tangra, specializing in relational database applications analysis, implementation, and tuning. His particular interest is in design patterns, performance a...

References



Comments
giribabu on 11/10/2008
thats great and helped me a lot to solve a problem thanks

Chris Shaw on 11/10/2008
That's a good piece of syntax I was unaware of. Excellent video.

Chris Shaw on 11/10/2008
That's a good piece of syntax I was unaware of. Excellent video.

Abhijit on 11/10/2008
Nice 1

Kevin Plotner on 11/10/2008
Very difficult to understand the presenter.

Marcin Zawadzki on 11/10/2008
Excelent - powerfull and elegant.

Marcin Zawadzki on 11/10/2008
Excelent - powerfull and elegant.

Douglas Kemp on 11/10/2008
Nice Tip !

Harvey Schwartz on 11/10/2008
I have been looking for an elegant solution like this for quite a while. Thank you.

Mohammad Hoque on 11/10/2008
Greate Video

Rizwan Gulamhussein on 11/10/2008
Excellent tip, thank you.

Munshi Verma on 11/12/2008
Good job!

Adam Gojdas on 11/24/2008
You may see an issue when the data has "<" and ">" within it. Since FOR XML will entity encode it you may not get what you were expecting. You will see "<" and ">". You will probably want to think about wrapping this with a REPLACE function to catch this.

Adam Gojdas on 11/24/2008
After reading a comment from here: http://sqlblog.com/blogs/adam_machanic/archive/2006/10/19/replacing-xp-execresultset-in-sql-server-2005.aspx I put together this demo for myself: --Easy way to concatenate multiple rows into a single string DECLARE @data table( someData varchar(255) NOT NULL PRIMARY KEY); INSERT INTO @data (someData) VALUES ('Apple'); INSERT INTO @data (someData) VALUES ('Pear'); INSERT INTO @data (someData) VALUES ('Pineapple'); INSERT INTO @data (someData) VALUES ('Grape'); INSERT INTO @data (someData) VALUES ('<Grape>''"'); --returns -- <Grape>''",Apple, Grape, Pear, Pineapple SELECT STUFF(( SELECT mydata.value('/row[1]/x[1]', 'varchar(max)') FROM ( SELECT x FROM ( SELECT ', ' + d.someData FROM @data d FOR XML PATH(''), TYPE ) y (x) FOR XML RAW, TYPE ) d (mydata) ), 1,2,'');

Adam Gojdas on 11/24/2008
Sorry for the way the above post came out. It was formatted but it seems to have stripped that all out.

Plamen Ratchev on 11/25/2008
Adam, This is very easy to handle. One way is to use the REPLACE function as you noted, but a better method is to change the XML type value to VARCHAR. That will return all original characters. Here is example: http://sqlblogcasts.com/blogs/tonyrogerson/archive/2006/07/06/871.aspx

Adam Gojdas on 11/25/2008
Plamen, Yes, exactly. The REPLACE method will have it's short comings. I did use a version of the method you mention. That is what the example above I added does. Note the - "mydata.value('/row[1]/x[1]', 'varchar(max)')". Although it does it at a slightly different place than the example you note. Thanks for the other link.

Plamen Ratchev on 11/26/2008
Adam, correct. I see now you had it in your example, the code wrapped around and I was looking at the end of the query for it.

Prasanna Prabhu on 11/28/2008
Sweet. Great video

Rahul on 11/29/2008
Excellent

Mike on 12/19/2008
New to SQL server, all the webpages explaining this method were not as clear as this video. Well done and thanks.

Excellant thanks

Christian Bahnsen on 6/12/2009
Great. Thanks a lot.

Dean Gross on 8/22/2009
great tip

Robert Schmehl on 10/16/2009
need more detail

Arun on 2/14/2010
very good

Cosmin Tornea on 4/16/2010
cool

Eduardo on 5/13/2010
very instructive!!!

vijayan on 6/2/2010
really useful



Must Be Logged In
 

How Do I Become a Video Author? |  Newsletter History

Copyright © Fourdeuce, Inc., 2005-2009. All Rights Reserved | Privacy Policy | Terms & Conditions