unable to switch the encoding error with XML Data type

If you ever tried to cast string to XML data type in SQL Server then you may encounter this error

select cast(xmldata as XML) from XMLFiles

Msg 9402, Level 16, State 1, Line 1

XML parsing: line 1, character 54, unable to switch the encoding

Solution is simple. Just convert UTF-8 to UTF-16 as below

select cast(Replace(xmldata,'UTF-8', 'UTF-16') as XML) from XMLFiles


Binary World is a Software Development company located in Atlanta, USA (since 2007). Binary World specialized in Business Intelligence, mobile, cloud computing and .Net Application Development.

Tagged with: , ,
Posted in SQL Server, T-SQL Tips

Leave a Reply