Solved

How to query xml from xml variable?

Posted on 2009-05-11
2
260 Views
Last Modified: 2013-11-05
Hello Experts,

I have a xml variable called @xmlstr in below code snippet.  

I need to extract customer xml portion (as mentioned below) from OrderInfo xml and store it in a table.

<Customer>
            <Name>First, Last</Name>
            <Address>xyz, zip</Address>
</Customer>



Please help me how can i extract sub-xml portion from xml?


Regards,
itsvtk
declare @xmlstr xml

set @xmlstr = 

'<OrderInfo>

	<Customer>

		<Name>First, Last</Name>

		<Address>xyz, zip</Address>

	</Customer>

	<Product>

		<Number>xyz123</Number>

		<Name>Product Name</Name>

	</Product>

</OrderInfo>'

Open in new window

0
Comment
Question by:Thandava Vallepalli
2 Comments
 
LVL 75

Accepted Solution

by:
Anthony Perkins earned 500 total points
ID: 24357026
Select @xmlstr.query('(/OrderInfo/Customer)[1]')
0
 
LVL 75

Expert Comment

by:Aneesh Retnakaran
ID: 24357029
SELECT A.Customers.value('(Customer/Name)[1]','varchar(40)') name,
         A.Customers.value('(Customer/Address)[1]','varchar(100)') Address,
         A.Customers.value('(Product/Number)[1]','varchar(100)') ProductNumber,
         A.Customers.value('(Product/Name)[1]','varchar(100)') ProductName
FROM @xmlstr.nodes('/OrderInfo') A(Customers)
0

Featured Post

Zoho SalesIQ

Hassle-free live chat software re-imagined for business growth. 2 users, always free.

Join & Write a Comment

In this article I will describe the Detach & Attach method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now