Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does SQL display an & as &? [duplicate]

Tags:

sql

t-sql

Possible Duplicate:
help with FOR XML PATH('') escaping “special” characters

I need some assistance, my query is below:

STUFF(
    (
      SELECT ',' + CountDesc
      FROM Count INNER JOIN ProjectCount ON Count.Id =  ProjectCount.CountId 
      WHERE ProjectCount.ProjectId = Project.Id ORDER BY Count.CountDesc
      FOR XML PATH('')
    ), 1, 1, '') as  [Country]

What happens is when i run this query and the Count table has an & in one of its fields, it displays the & as &.

Is there anyway to not let this happen?

Thanks in advance.

like image 473
johnnie Avatar asked Nov 07 '25 14:11

johnnie


2 Answers

It is happening because the strings being combined in the XML statement are using XML specific characters. In addition to &, the also affects < and >, and probably other characters.

I usually fix this be doing a replace after the call:

select @str = replace(@str, '&amp;', '&')

And nesting the replaces for additional characters.

like image 124
Gordon Linoff Avatar answered Nov 10 '25 04:11

Gordon Linoff


Per Section 2.4 of the XML spec, & must be escaped except for in a few special cases (e.g. within a comment or CDATA section). If the & wasn't displayed as &amp;, the XML would be invalid.

like image 32
goric Avatar answered Nov 10 '25 03:11

goric



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!