Link to home
Start Free TrialLog in
Avatar of Thandava Vallepalli
Thandava VallepalliFlag for United States of America

asked on

How to query xml from xml variable?

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

ASKER CERTIFIED SOLUTION
Avatar of Anthony Perkins
Anthony Perkins
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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)