Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use FOR XML PATH('') in a query without escaping special characters?

I have this query:

SELECT DISTINCT
            f.CourseEventKey,
            (
                SELECT      f.Title + '; ' AS [text()]
                FROM        @Facilities
                WHERE       CourseEventKey = f.CourseEventKey
                ORDER BY    f.Title
                FOR XML PATH('')
            ) Facilities
FROM        @Facilities f

It produces this result set:

CourseEventKey Facilities
-------------- -----------------------------------
29             Test Facility 1; 
30             Memphis Training Room; 
32             Drury Inn & Suites Creve Coeur;

The data is fine, but the & is actually an encoded &, which is not suitable for my purposes.

How can I modify this query to return the original values of the special characters in my data?

like image 809
keeehlan Avatar asked Apr 17 '14 17:04

keeehlan


People also ask

How do I escape a special character in SQL query?

Escape CharactersUse the backslash character to escape a single character or symbol. Only the character immediately following the backslash is escaped.

What characters need to be escaped SQL?

%, _, [, ], and ^ need to be escaped, and you will need to choose a suitable escape character, i.e. one that you aren't using elsewhere in your LIKE pattern.

What is use of stuff in SQL Server?

The STUFF function inserts a string into another string. It deletes a specified length of characters in the first string at the start position and then inserts the second string into the first string at the start position.


3 Answers

I think you're going to have to manually wrap the Facilities inline query block with REPLACE statements to reverse the automatic escaping.

It sounds like what you're wanting to do is concatenate multiple facilities that could present a given course. Have you considered other options? This question has several possible approaches that don't have an issue with escaping your characters.

like image 80
mwigdahl Avatar answered Oct 18 '22 12:10

mwigdahl


Use ,TYPE).value('.','NVARCHAR(MAX)') and your special characters will not be escaped:

SELECT DISTINCT
            f.CourseEventKey,
            (
                SELECT      f.Title + '; ' AS [text()]
                FROM        @Facilities
                WHERE       CourseEventKey = f.CourseEventKey
                ORDER BY    f.Title
                FOR XML PATH(''),TYPE).value('.','NVARCHAR(MAX)')
            AS Facilities
FROM        @Facilities f

Credit for this goes to Rob Farley.

UPDATE:
I just heard about this new method. I haven't tested it thoroughly yet, and would appreciate any feedback. We can replace [text()] with [processing-instruction(x)], like this

select 'hello & there >' as [processing-instruction(x)] FOR XML PATH('')

will return

<?x hello & there >?>

We just need to strip off the <? ... ?>

like image 22
Baodad Avatar answered Oct 18 '22 12:10

Baodad


Adding to the latest solution provided by @Baodad, simply REPLACE the <? ... ?>.

SELECT DISTINCT
        f.CourseEventKey,
        REPLACE(
        REPLACE(
                (
                SELECT f.Title + '; ' AS [processing-instruction(x)]
                FROM Facilities
                WHERE CourseEventKey = f.CourseEventKey
                ORDER BY f.Title
                FOR XML PATH('')
                )
                , '<?x','')
                , '?>','') Facilities
FROM Facilities f`

Your output will be:

| CourseEventKey |                                 Facilities |
|----------------|--------------------------------------------|
|             29 |        Test Facility 1;  Test Facility 1;  |
|             29 |      Test Facility 33;  Test Facility 33;  |
|             30 |                    Memphis Training Room;  |
|             31 |                            Another place;  |
|             32 |  Drury Inn & Suites;  Drury Inn & Suites;  |
|             32 |    Yet Another place;  Yet Another place;  |

This works because your final output is not in XML, otherwise REPLACE would simply not work as the characters would remain encoded.

like image 41
Struco Avatar answered Oct 18 '22 13:10

Struco