Solved

Concatenate multiple rows using XML PATH leaves special characters

Posted on 2014-02-20
8
1,561 Views
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?

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

Expert Comment

by:Surendra Nath
Comment Utility
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

OPEN @T
FETCH @T INTO @Comments

WHILE (@@FETCH_STATUS  = 0)
BEGIN

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

END

Close @T
DEALLOCATE @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.
0
 

Author Comment

by:Lensolo
Comment Utility
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.
0
 

Author Comment

by:Lensolo
Comment Utility
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
0
 
LVL 16

Expert Comment

by:Surendra Nath
Comment Utility
0
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 

Author Comment

by:Lensolo
Comment Utility
yeah. That's what I thought they should be, but if you look above, that's not the characters sql returns.
0
 
LVL 16

Accepted Solution

by:
Surendra Nath earned 500 total points
Comment Utility
0
 

Author Comment

by:Lensolo
Comment Utility
Wow. Excellent. Thank you very much!
0
 

Author Closing Comment

by:Lensolo
Comment Utility
Very thorough, helpful & quick to respond. Thank you very much.
0

Featured Post

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Join & Write a Comment

In this article I will describe the Detach & Attach 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.
In this article I will describe the Backup & Restore 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.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

743 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now