Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

How to query xml from xml variable?

Posted on 2009-05-11
2
264 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

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

In this article I will describe the Backup & Restore 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.
I have a large data set and a SSIS package. How can I load this file in multi threading?
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.

856 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