Escaping ampersand in FOR XML

  • 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?

  • 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.

  • 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

    SQLServerNewbieMCITP: Database Administrator SQL Server 2005
  • My resolution to this issue was to use %26 in the database for an ampersand, then replace it on the client end with '&'

Viewing 4 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic. Login to reply