Ian Beckett

RSS feed

    Recent comments

    Authors

    SQL HTML Ampersand to character conversion database function

    In my web crawling adventures I have the recurring need to translate HTML Ampersand codes into the actual characters they represent.   In this post you will find the user defined function that I use in SQL Server 2008 to handle this requirement. 

    I have also included a copy of the conversion table for your viewing pleasure.  Please let me know if you find any Ampersand commands that are missing from the mapping table! 

    Enjoy!

    Usage:

    SELECT 'Death ' + dbo.ReplaceAmpersands('&') + ' Taxes'

    Output: Death & Taxes

    T-SQL to create the function:

    CREATE FUNCTION dbo.ReplaceAmpersands
    (
     @input nvarchar(MAX)
    )
    RETURNS NVARCHAR(MAX)
    AS
    BEGIN
     SELECT @input = REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(
         REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(
         REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(
         REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(
         REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(
         REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(
         REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(
         REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(
         REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(
         REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(
         REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(
         REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(
         REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(
         REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(
         REPLACE(REPLACE(REPLACE(@input,'&lt;','<'),'&gt;','>'),
         '&amp;','&'),'&quot;','"'),'&nbsp;',' '),'&Ccedil;','Ç'),
         '&ccdil;','ç'),'&Ntilde;','Ñ'),'&ntilde;','ñ'),'&THORN;','Þ'),
         '&thorn;','þ'),'&Yacute;','Ý'),'&yacute;','ý'),'&yuml;','ÿ'),
         '&szlig;','ß'),'&AElig;','Æ'),'&Aacute;','Á'),'&Acirc;','Â'),
         '&Agrave;','À'),'&Aring;','Å'),'&Atilde;','Ã'),'&Auml;','Ä'),
         '&aelig;','æ'),'&aacute;','á'),'&acirc;','â'),'&agrave;','à'),
         '&aring;','å'),'&atilde;','ã'),'&auml;','ä'),'&ETH;','Ð'),
         '&Eacute;','É'),'&Ecirc;','Ê'),'&Egrave;','È'),'&Euml;','Ë'),
         '&eth;','ð'),'&eacute;','é'),'&ecirc;','ê'),'&egrave;','è'),
         '&euml;','ë'),'&Iacute;','Í'),'&Icirc;','Î'),'&Igrave;','Ì'),
         '&Iuml;','Ï'),'&iacute;','í'),'&icirc;','î'),'&igrave;','ì'),
         '&iuml;','ï'),'&Oacute;','Ó'),'&Ocirc;','Ô'),'&Ograve;','Ò'),
         '&Oslash;','Ø'),'&Otilde;','Õ'),'&sect;','§'),'&laquo;','«'),
         '&raquo;','»'),'&sup3;','³'),'&iquest;','¿'),'&deg;','°'),
         '&brvbar;','¦'),'&frac12;','½'),'&frac34;','¾'),'&sup1;','¹'),
         '&sup2;','²'),'&cent;','¢'),'&pound;','£'),'&yen;','¥'),
         '&frac14;','¼'),'&plusmn;','±'),'&micro;','µ'),'&para;','¶'),
         '&middot;','·'),'&ucirc;','û'),'&ugrave;','ù'),'&uuml;','ü'),
         '&reg;','®'),'&Ucirc;','Û'),'&Ugrave;','Ù'),'&Uuml;','Ü'),
         '&uacute;','ú'),'&oslash;','ø'),'&otilde;','õ'),'&ouml;','ö'),
         '&Uacute;','Ú'),'&Ouml;','Ö'),'&oacute;','ó'),'&ocirc;','ô'),'&ograve;','ò');
     RETURN @input
    END

     

    And here is the actual conversion table that was used to build this function:

    AmpCommand AmpChar
    &lt; <
    &gt; >
    &amp; &
    &quot; "
    &nbsp;  
    &Ccedil; Ç
    &ccdil; ç
    &Ntilde; Ñ
    &ntilde; ñ
    &THORN; Þ
    &thorn; þ
    &Yacute; Ý
    &yacute; ý
    &yuml; ÿ
    &szlig; ß
    &AElig; Æ
    &Aacute; Á
    &Acirc; Â
    &Agrave; À
    &Aring; Å
    &Atilde; Ã
    &Auml; Ä
    &aelig; æ
    &aacute; á
    &acirc; â
    &agrave; à
    &aring; å
    &atilde; ã
    &auml; ä
    &ETH; Ð
    &Eacute; É
    &Ecirc; Ê
    &Egrave; È
    &Euml; Ë
    &eth; ð
    &eacute; é
    &ecirc; ê
    &egrave; è
    &euml; ë
    &Iacute; Í
    &Icirc; Î
    &Igrave; Ì
    &Iuml; Ï
    &iacute; í
    &icirc; î
    &igrave; ì
    &iuml; ï
    &Oacute; Ó
    &Ocirc; Ô
    &Ograve; Ò
    &Oslash; Ø
    &Otilde; Õ
    &sect; §
    &laquo; «
    &raquo; »
    &sup3; ³
    &iquest; ¿
    &deg; °
    &brvbar; ¦
    &frac12; ½
    &frac34; ¾
    &sup1; ¹
    &sup2; ²
    &cent; ¢
    &pound; £
    &yen; ¥
    &frac14; ¼
    &plusmn; ±
    &micro; µ
    &para;
    &middot; ·
    &ucirc; û
    &ugrave; ù
    &uuml; ü
    &reg; ®
    &Ucirc; Û
    &Ugrave; Ù
    &Uuml; Ü
    &uacute; ú
    &oslash; ø
    &otilde; õ
    &ouml; ö
    &Uacute; Ú
    &Ouml; Ö
    &oacute; ó
    &ocirc; ô
    &ograve; ò


    Posted by ibeckett on Thursday, December 10, 2009 4:29 AM
    Permalink | Comments (0) | Post RSSRSS comment feed

    Related posts