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?
Escape CharactersUse the backslash character to escape a single character or symbol. Only the character immediately following the backslash is escaped.
%, _, [, ], 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.
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.
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.
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 <? ... ?>
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With