Fairly new to XML parsing in SQL Server. Here's what I have and what I'm trying to do.
I have a table with many rows similar to this:
+-------------------+------------------------------------+
| EDI_Assessment_ID | XML_TEXT |
+-------------------+------------------------------------+
| 12345 | text column containing XML |
| 12346 | text column containing XML |
+-------------------+------------------------------------+
The XML_Text column has a large XML text similar to this structure (i've simplified and only pasted the relevant portions of it:
<Assessment>
<ADLs>
<ADL_Group>
<ADL>bathing</ADL>
<Mapped_ADL Source="Calypso">Bathing</Mapped_ADL>
<ADL_Level>Requires only equipment to complete ADL</ADL_Level>
<Mapped_ADL_Level Source="Calypso">Independent</Mapped_ADL_Level>
<ADL_Equipment>HH shower</ADL_Equipment>
<ADL_Assisted_By_Info>
<ADL_Assisted_By>No one</ADL_Assisted_By>
</ADL_Assisted_By_Info>
</ADL_Group>
<ADL_Group>
<ADL>Continence-Bowel</ADL>
<Mapped_ADL Source="Calypso">Continence</Mapped_ADL>
<ADL_Level>Independent</ADL_Level>
<Mapped_ADL_Level Source="B/A">Independent</Mapped_ADL_Level>
<ADL_Equipment />
<ADL_Assisted_By_Info>
<ADL_Assisted_By>No one</ADL_Assisted_By>
</ADL_Assisted_By_Info>
</ADL_Group>
</Assessment>
How can i parse through the XML for each row in the table to return:
- The ADL (bathing, Continence-Bowel) and
- the ADL_Assisted_By_Info
I'm looking for the result set to return similar to this:
+-------------------+-------------+----------------------+------------------+----------------------+
| EDI_Assessment_ID | Bathing | ADL_Assisted_By_Info | Continence-Bowel | ADL_Assisted_By_Info |
+-------------------+-------------+----------------------+------------------+----------------------+
| 12345 | Independent | No one | Independent | No one |
+-------------------+-------------+----------------------+------------------+----------------------+
varchar(MAX)to store xml and not thexmldatatype? You can't use XQUERY on avarchar, so that's automatically making this task harder that it needs to be. if the reason is because the XML isn't valid xml (so, when trying to store it as anxmlyou get an error), then this is far far harder. Ideally you should be fixing the XML first so that it's valid.texthas been deprecated since at least SQL Server 2005. That doesn't answer by question though. Why are you usingvarchar(MAX)/text? There's anxmldatatype and XML should be stored asxml.CONVERTthe value ofXML_TEXTtoxmlevery time you want to use XQUERY against it. That's going to slow the query down, as the data engine will have to validate every row you're addressing to ensure it is valid XML.