Convert string into xml and insert Sql Server

  • We have a sql server 2008 R2 database table with an xml stored in a column as a VARCHAR data type.

    I now have to fetch some of the elements of the xml. So I want to first convert the xml stored as a VARCHAR data type, to an xml stored as an xml data type.

    example : Table A Id(int) , ProductXML (varchar(max))

    Table B

    Id(int), ProductXML(XML)

    I want to convert the ProductXML from Table A into XML data type and insert into Table B.

    I tried using the CAST() and CONVERT() function as shown below :

    insert into TableB (ProductXML)

    select CAST(ProductXML as XML) from TableA;

    similarly tried convert but I get an error 'XML Parsing : unable to switch encoding'.

    Is there any way I can convert the varchar entries in the table into xml entries ?

    About the *XML* : The XML is huge with many nodes , and its structure changes dynamically. Example : One row can have and XML entry for 1 product and another row can have an xml entry for multiple products.

  • You did a pretty good job posting details about your issue. It would be easier if you had created tables but from your description it is pretty simple.

    create table #TableA

    (

    ID int identity primary key,

    ProductXML varchar(max)

    )

    create table #TableB

    (

    ID int identity primary key,

    ProductXML xml

    )

    What we can't see though is what does the existing data look like in TableA? Can you provide a few rows of sample data? I have an idea of what is wrong but once I see the original data I will know for sure.

    FWIW, if you have XML (or any other datatype) that you intend to use as that datatype it is FAR better to use the appropriate datatype. If your original data was an XML datatype this wouldn't be an issue. 😛

    _______________________________________________________________

    Need help? Help us help you.

    Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

    Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.

    Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
    Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
    Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
    Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/

  • Hi Sean,

    Thank you for your reply. This is the second time you are helping me out.

    I actually see that there is

    <?xml version="1.0" encoding="utf-16"?>

    part in the xml. This is what is causing the error.

    Once I remove this from the xml I dont see the error.

    But I wanted to know if we can convert the xml without

    removing this part of the xml.

  • SqlServerNinja (4/11/2013)


    Hi Sean,

    Thank you for your reply. This is the second time you are helping me out.

    I actually see that there is

    <?xml version="1.0" encoding="utf-16"?>

    part in the xml. This is what is causing the error.

    Once I remove this from the xml I dont see the error.

    But I wanted to know if we can convert the xml without

    removing this part of the xml.

    That is exactly what I thought. You took XML that is encoded as UNICODE and stuck it into a non-UNICODE datatype (varchar). To get around this you will have to first cast your non-UNICODE datatype to a valid UNICODE type.

    Something like this should work.

    select CAST(CAST(ProductXML as nvarchar(max)) as XML) from TableA;

    _______________________________________________________________

    Need help? Help us help you.

    Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

    Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.

    Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
    Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
    Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
    Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/

  • Thats great ! Thank you ! It works !

Viewing 5 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic. Login to reply