问题
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
('"','"'),('&','&'),(''',''''),('<','<'),('>','>'),(' ',' '),('¡','¡'),
('¢','¢'),('£','£'),('¤','¤'),('¥','¥'),('¦','¦'),('§','§'),('¨','¨'),
('©','©'),('ª','ª'),('«','«'),('¬','¬'),('®','®'),('¯','¯'),('°','°'),
('±','±'),('²','²'),('³','³'),('´','´'),('µ','µ'),('¶','¶'),('·','·'),
('¸','¸'),('¹','¹'),('º','º'),('»','»'),('¼','¼'),('½','½'),('¾','¾'),
('¿','¿'),('À','À'),('Á','Á'),('Â','Â'),('Ã','Ã'),('Ä','Ä'),('Å','Å'),
('Æ','Æ'),('Ç','Ç'),('È','È'),('É','É'),('Ê','Ê'),('Ë','Ë'),('Ì','Ì'),
('Í','Í'),('Î','Î'),('Ï','Ï'),('Ð','Ð'),('Ñ','Ñ'),('Ò','Ò'),('Ó','Ó'),
('Ô','Ô'),('Õ','Õ'),('Ö','Ö'),('×','×'),('Ø','Ø'),('Ù','Ù'),('Ú','Ú'),
('Û','Û'),('Ü','Ü'),('Ý','Ý'),('Þ','Þ'),('ß','ß'),('à','à'),('á','á'),
('&;','â'),('ã','ã'),('ä','ä'),('å','å'),('æ','æ'),('ç','ç'),('è','è'),
('é','é'),('ê','ê'),('ë','ë'),('ì','ì'),('í','í'),('î','î'),('ï','ï'),
('ð','ð'),('ñ','ñ'),('ò','ò'),('ó','ó'),('ô','ô'),('õ','õ'),('ö','ö'),
('÷','÷'),('ø','ø'),('ù','ù'),('ú','ú'),('û','û'),('ü','ü'),('ý','ý'),
('þ','þ'),('ÿ','ÿ'),('&','&'),('°','°'),('∞','∞'),('‰','‰'),('⋅','⋅'),
('±','±'),('†','†'),('—','—'),('¬','¬'),('µ','µ'),('€','€'),('£','£'),
('¥','¥'),('¢','¢'),('€','€'),('£','£'),('¥','¥'),('¢','¢')
) A (MapFrom,MapTo)
Select cast(cast(@S as xml) as nvarchar(max))
Returns
sweet shop £ -- added a space ( ) and a 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('sweet shop')
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