Concatenate multiple rows using XML PATH leaves special characters

Posted on 2014-02-20
Last Modified: 2014-02-20
I am trying to find a way to concatenate comments from multiple rows in a view. The best way I can get it to work is using XML PATH, but it converts special characters to "&#gt;" or "
", & ";&".

Is there a way to either clean up the special characters (not using replace - I want to know that I got them all) or is there another way to do this in a view?

          Stuff((SELECT Comments
                     FROM TableName A
                     WHERE B.ID = A.ID
                     FOR XML PATH ('')),1,0,'')
FROM   TableName B
Question by:Lensolo
  • 5
  • 3
LVL 16

Expert Comment

by:Surendra Nath
ID: 39875179
No, there is no way to clean the special characters without using the replace function.
is there an other way to do this on a view, yes definetly but it may not be a as performant as the XML path thing.

the old school method:

declare @T CURSOR FOR
SELECT Comments
                     FROM TableName A,TableName B
                     WHERE B.ID = A.ID

FETCH @T INTO @Comments


SELECT @CONCAT = @CONCAT + @comments
FETCH @T INTO @Comments


Close @T

now the above method certainly works, but it will hit a performance bottleneck when there is a huge load..

so we suggest to go with the replace + xm path instead.

Author Comment

ID: 39875199
Thanks. Is there a list of all possible values I have to clean up (or if you can tell me what these are called I can google it)? If that's the cleanest option, I'll do it. I just need to make sure I have a full list.

Author Comment

ID: 39875202
Also, I found commenst that TYPE can be added to the end of atleast XML AUTO to fix this, but I can't seem to find a way to get this added to the end of XML PATH
NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

LVL 16

Expert Comment

by:Surendra Nath
ID: 39875221

Author Comment

ID: 39875226
yeah. That's what I thought they should be, but if you look above, that's not the characters sql returns.
LVL 16

Accepted Solution

Surendra Nath earned 500 total points
ID: 39875228

Author Comment

ID: 39875232
Wow. Excellent. Thank you very much!

Author Closing Comment

ID: 39875237
Very thorough, helpful & quick to respond. Thank you very much.

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
replicated - directional or bidirectional? 3 29
convert null in sql server 12 34
Sql Server group by 10 27
Find results from sql within a time span 11 30
In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question