Unable to cast TEXT to XML in SQL Server

后端 未结 6 772
自闭症患者
自闭症患者 2020-12-25 10:36

Basically I have a column named XML that is of type TEXT; this cannot be changed for other reason, but I was wondering how I could cast it to XML.<

相关标签:
6条回答
  • 2020-12-25 11:18

    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 .....

    0 讨论(0)
  • 2020-12-25 11:23

    You should replace encoding="utf-16" to encoding="utf-8" or ''(blank) and then perform your operation.

    a. Converting encoding="utf-16" to encoding="utf-8"

    SELECT 
      CAST(
        REPLACE(CAST([xml] AS VARCHAR(MAX)), 'encoding="utf-16"', 'encoding="utf-8"')
      AS XML).value('(/Record//UserGuid/node())[1]', 'NVARCHAR(max)') as UserGuid
    from tbl_Module_RequestForms_Items
    

    b. Replacing encoding="utf-16" to ''(blank)

    SELECT 
      CAST(
        REPLACE(CAST([xml] AS VARCHAR(MAX)), 'encoding="utf-16"', '')
      AS XML).value('(/Record//UserGuid/node())[1]', 'NVARCHAR(max)') as UserGuid
    from tbl_Module_RequestForms_Items
    
    0 讨论(0)
  • 2020-12-25 11:26

    Replacing encoding="utf-8" to encoding="utf-16" worked for me :)

    0 讨论(0)
  • 2020-12-25 11:30

    You need to change the encoding before casting to XML.

    CAST (REPLACE(MyTextToCastToXML, 'utf-8', 'utf-16') AS XML)
    
    0 讨论(0)
  • 2020-12-25 11:31

    Have you tried CONVERT instead of CAST?

    SELECT CONVERT(XML, @xml).value('(/Record/UserGuid)[1]', 'NVARCHAR(max)')
    from tbl_Module_RequestForms_Items
    

    Also, check out the "xml styles" section of this page; it contains some options you have when converting xml:

    http://msdn.microsoft.com/en-us/library/ms187928.aspx

    0 讨论(0)
  • 2020-12-25 11:33

    Casting XML variable as NTEXT solves the problem CAST(CAST (XML AS NTEXT) AS XML).

    0 讨论(0)
提交回复
热议问题