Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Convert string into xml and insert Sql Server Expand / Collapse
Author
Message
Posted Thursday, April 11, 2013 11:34 AM
Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

Group: General Forum Members
Last Login: Tuesday, February 18, 2014 5:22 PM
Points: 58, Visits: 197
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.
Post #1441396
Posted Thursday, April 11, 2013 12:14 PM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 2:49 PM
Points: 12,016, Visits: 11,046
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 Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Post #1441414
Posted Thursday, April 11, 2013 12:58 PM
Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

Group: General Forum Members
Last Login: Tuesday, February 18, 2014 5:22 PM
Points: 58, Visits: 197
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.

Post #1441444
Posted Thursday, April 11, 2013 1:06 PM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 2:49 PM
Points: 12,016, Visits: 11,046
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 Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Post #1441449
Posted Thursday, April 11, 2013 2:45 PM
Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

Group: General Forum Members
Last Login: Tuesday, February 18, 2014 5:22 PM
Points: 58, Visits: 197
Thats great ! Thank you ! It works !
Post #1441483
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse