How to decode XML entities in SQL?

邮差的信 提交于 2019-12-11 12:17:50

问题


How can I convert/decode a text in غلامحسين format to normal text? I'm using SQL server 2012. I just want to update all of them to normal text.


回答1:


If you know Firstname and Lastname are valid when interpreted as XML, then this will do:

UPDATE _table_
SET Firstname = CONVERT(NVARCHAR(MAX), CONVERT(XML, FirstName)), 
Lastname = CONVERT(NVARCHAR(MAX), CONVERT(XML, LastName))

The XML parser will decode the entities for you.




回答2:


For some reason Jeroen's answer doesn't seem to work for me in SQL Server 2016.

The function below works... same principle, just executed differently.

create function dbo.xmlDecode (@string nvarchar(max))
returns varchar(max)
begin
    return cast(@string as XML).value('.[1]','nvarchar(max)' )
end;


来源:https://stackoverflow.com/questions/28504103/how-to-decode-xml-entities-in-sql

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