I have a xml docment structure which looks like the following
<X>
<Y>Noah</Y>
<Y>111 Fake St</Y>
<Y>888-555-5555</Y>
</X>
<X>
<Y>Jonh</Y>
<Y>123 Jump St</Y>
<Y>888-500-5000</Y>
</X>
<X>
.
.
.
</X>
The above is an example of one row of data in the table column code_xml
I was having trouble figuring out how to break the X nodes into separate rows and making Y nodes into their appropriate columns like the following:
I was reviewing the XML documentation and trying to use the different Sql Server XML Methods such as nodes and value but was running into a wall.
I felt as though using a cross apply using the nodes function to break up each inner X element, then using the value method on the Y elements with their appropriate positional value would do the trick.
I was failing because I was not using the proper dot notation as seen below:
select
d.p.value('./Y[1]', 'varchar(200)') Name,
p.value('./Y[2]', 'varchar(200)') Address,
p.value('./Y[3]', 'varchar(200)') Phone
from
T
cross apply
T.Code_xml.nodes('/X') as d(p)
Then I found this Stack Overflow post and helped me solve my issue.