How to decode html encoded text in sql server? (or ms access!)

夙愿已清 提交于 2020-01-14 19:20:08

问题


I have a column with text in the following format...

sweet shop

is there a way to convert this directly to it's corresponding text in sql server? (it is actually a linked ms access database so I could also use access too!)

(I think this format is also called Numeric character reference and contains the code points of unicode characters)


回答1:


Alex K is 99.99% correct, however the conversion would fail if you had Named Codes like   or £

So, here we perform a brute force replace, and then parse the string via XML

Example

Declare @S nvarchar(max) = 'sweet shop £'

Select @S = replace(@S,MapFrom,MapTo)
 From  ( values
        ('&quot;','"'),('&amp;','&'),('&apos;',''''),('&lt;','<'),('&gt;','>'),('&nbsp;',' '),('&iexcl;','¡'),
        ('&cent;','¢'),('&pound;','£'),('&curren;','¤'),('&yen;','¥'),('&brvbar;','¦'),('&sect;','§'),('&uml;','¨'),
        ('&copy;','©'),('&ordf;','ª'),('&laquo;','«'),('&not;','¬'),('&reg;','®'),('&macr;','¯'),('&deg;','°'),
        ('&plusmn;','±'),('&sup2;','²'),('&sup3;','³'),('&acute;','´'),('&micro;','µ'),('&para;','¶'),('&middot;','·'),
        ('&cedil;','¸'),('&sup1;','¹'),('&ordm;','º'),('&raquo;','»'),('&frac14;','¼'),('&frac12;','½'),('&frac34;','¾'),
        ('&iquest;','¿'),('&Agrave;','À'),('&Aacute;','Á'),('&Acirc;','Â'),('&Atilde;','Ã'),('&Auml;','Ä'),('&Aring;','Å'),
        ('&AElig;','Æ'),('&Ccedil;','Ç'),('&Egrave;','È'),('&Eacute;','É'),('&Ecirc;','Ê'),('&Euml;','Ë'),('&Igrave;','Ì'),
        ('&Iacute;','Í'),('&Icirc;','Î'),('&Iuml;','Ï'),('&ETH;','Ð'),('&Ntilde;','Ñ'),('&Ograve;','Ò'),('&Oacute;','Ó'),
        ('&Ocirc;','Ô'),('&Otilde;','Õ'),('&Ouml;','Ö'),('&times;','×'),('&Oslash;','Ø'),('&Ugrave;','Ù'),('&Uacute;','Ú'),
        ('&Ucirc;','Û'),('&Uuml;','Ü'),('&Yacute;','Ý'),('&THORN;','Þ'),('&szlig;','ß'),('&agrave;','à'),('&aacute;','á'),
        ('&;','â'),('&atilde;','ã'),('&auml;','ä'),('&aring;','å'),('&aelig;','æ'),('&ccedil;','ç'),('&egrave;','è'),
        ('&eacute;','é'),('&ecirc;','ê'),('&euml;','ë'),('&igrave;','ì'),('&iacute;','í'),('&icirc;','î'),('&iuml;','ï'),
        ('&eth;','ð'),('&ntilde;','ñ'),('&ograve;','ò'),('&oacute;','ó'),('&ocirc;','ô'),('&otilde;','õ'),('&ouml;','ö'),
        ('&divide;','÷'),('&oslash;','ø'),('&ugrave;','ù'),('&uacute;','ú'),('&ucirc;','û'),('&uuml;','ü'),('&yacute;','ý'),
        ('&thorn;','þ'),('&yuml;','ÿ'),('&amp;','&'),('&deg;','°'),('&infin;','∞'),('&permil;','‰'),('&sdot;','⋅'),
        ('&plusmn;','±'),('&dagger;','†'),('&mdash;','—'),('&not;','¬'),('&micro;','µ'),('&euro;','€'),('&pound;','£'),
        ('&yen;','¥'),('&cent;','¢'),('&euro;','€'),('&pound;','£'),('&yen;','¥'),('&cent;','¢')
       ) A (MapFrom,MapTo)

Select cast(cast(@S as xml) as nvarchar(max))

Returns

sweet shop £  -- added a space (&nbsp;) and a Pound (&pound;) symbol to the original string



回答2:


Here is a much easier way to decode HTML-encoded strings:

It requires SQLHTTP which is a free database/assembly that we created which you can find on our website at: http://sqlhttp.net/documentation/encoding/htmldecode/

SELECT SQLHTTP.net.HtmlDecode('&#115;&#119;&#101;&#101;&#116;&#32;&#115;&#104;&#111;&#112;')

and the result is as expected:

-------------
sweet shop

(1 row affected)


来源:https://stackoverflow.com/questions/49817637/how-to-decode-html-encoded-text-in-sql-server-or-ms-access

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!