Unable to cast TEXT to XML in SQL Server

Your problem is: you have XML with an encoding="utf-16", but your column is a non-Unicode column……

Assuming that you cannot change it to NTEXT either, you have to do two nested CAST to achieve what you’re looking for:

SELECT 
    CAST(CAST(XML AS NTEXT) AS XML).value('(/Record/UserGuid)[1]', 'NVARCHAR(max)')
FROM 
    tbl_Module_RequestForms_Items

First, you need to cast to NTEXT (or NVARCHAR(MAX)), and then you have to cast that result to XML, before you can use it.

Tip: remove those “other reasons” and convert this to XML datatype if you really need to use it as XML …..

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)