Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Escaping ampersand in FOR XML


Escaping ampersand in FOR XML

Author
Message
Eric L Hackett
Eric L Hackett
Valued Member
Valued Member (72 reputation)Valued Member (72 reputation)Valued Member (72 reputation)Valued Member (72 reputation)Valued Member (72 reputation)Valued Member (72 reputation)Valued Member (72 reputation)Valued Member (72 reputation)

Group: General Forum Members
Points: 72 Visits: 211
Does anybody know a way to prevent SQL 2005 from entitizing the ampersand in a FOR XML statement?

Ex:
SELECT 'joe&bob@mail.com' + ';' FOR XML PATH('')
returns 'joe&bob@mail.com;' and I want to just get the '&' no 'amp;'



I'm using this with a contact table of email addresses. Based on different criteria a UNION of SELECTed email addresses becomes a semicolon-delimited string, nicely concatenated by FOR XML at the end of all the UNIONs. This is used by a client application that splits the string into email address, delimiting on the semicolon. Obviously the additional semicolon in the middle of an address causes problems.

Is there an 'inline' method for telling SQL to not entitize the ampersand?
Eric L Hackett
Eric L Hackett
Valued Member
Valued Member (72 reputation)Valued Member (72 reputation)Valued Member (72 reputation)Valued Member (72 reputation)Valued Member (72 reputation)Valued Member (72 reputation)Valued Member (72 reputation)Valued Member (72 reputation)

Group: General Forum Members
Points: 72 Visits: 211
It looks like IE and FF display this differently.
Basically I want the result of FOR XML not to have any 'amp;' in the result when an ampersand is present.
Jerry Hung
Jerry Hung
Right there with Babe
Right there with Babe (794 reputation)Right there with Babe (794 reputation)Right there with Babe (794 reputation)Right there with Babe (794 reputation)Right there with Babe (794 reputation)Right there with Babe (794 reputation)Right there with Babe (794 reputation)Right there with Babe (794 reputation)

Group: General Forum Members
Points: 794 Visits: 1208
I don't know anyway around how to escape/preserve the special characters in XML, since they indeed are SPECIAL
like &, , etc...
http://www.xml.com/pub/a/2001/01/31/qanda.html


Someone asked this question on StackOverflow (Google)
http://stackoverflow.com/questions/270948/how-to-preserve-an-ampersand-while-using-for-xml-path-on-sql-2005

SQLServerNewbie

MCITP: Database Administrator SQL Server 2005
Eric L Hackett
Eric L Hackett
Valued Member
Valued Member (72 reputation)Valued Member (72 reputation)Valued Member (72 reputation)Valued Member (72 reputation)Valued Member (72 reputation)Valued Member (72 reputation)Valued Member (72 reputation)Valued Member (72 reputation)

Group: General Forum Members
Points: 72 Visits: 211
My resolution to this issue was to use %26 in the database for an ampersand, then replace it on the client end with '&'
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search