SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Convert string into xml and insert Sql Server


Convert string into xml and insert Sql Server

Author
Message
SqlServerNinja
SqlServerNinja
SSC Journeyman
SSC Journeyman (80 reputation)SSC Journeyman (80 reputation)SSC Journeyman (80 reputation)SSC Journeyman (80 reputation)SSC Journeyman (80 reputation)SSC Journeyman (80 reputation)SSC Journeyman (80 reputation)SSC Journeyman (80 reputation)

Group: General Forum Members
Points: 80 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.
Sean Lange
Sean Lange
One Orange Chip
One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)

Group: General Forum Members
Points: 26173 Visits: 17539
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. :-P

_______________________________________________________________

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.

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)
SqlServerNinja
SqlServerNinja
SSC Journeyman
SSC Journeyman (80 reputation)SSC Journeyman (80 reputation)SSC Journeyman (80 reputation)SSC Journeyman (80 reputation)SSC Journeyman (80 reputation)SSC Journeyman (80 reputation)SSC Journeyman (80 reputation)SSC Journeyman (80 reputation)

Group: General Forum Members
Points: 80 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.
Sean Lange
Sean Lange
One Orange Chip
One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)

Group: General Forum Members
Points: 26173 Visits: 17539
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.

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)
SqlServerNinja
SqlServerNinja
SSC Journeyman
SSC Journeyman (80 reputation)SSC Journeyman (80 reputation)SSC Journeyman (80 reputation)SSC Journeyman (80 reputation)SSC Journeyman (80 reputation)SSC Journeyman (80 reputation)SSC Journeyman (80 reputation)SSC Journeyman (80 reputation)

Group: General Forum Members
Points: 80 Visits: 197
Thats great ! Thank you ! It works !
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search