Click here to Skip to main content
15,903,798 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I want to parse xml string with child nodes having same name while fetching from a table in sql. Below is the xml string and I want to fetch value in the DebitAccountnumber nodes. below fetch query works only for distinct node names
My xml string is as below:
            <DebitNarration>Net Settlement 11.11.22</DebitNarration>
            <DebitNarration>Mirror Ac 11.11.22;</DebitNarration>
            <CreditNarration>Net Settlement 11.11.22</CreditNarration>
         <BranchCode />

What I have tried:

select CAST(CAST(REQ.MQXMLdata AS XML).query('data(/*/Request/DebitAccount/DebitAccountNo)') AS VARCHAR(50)) AS DebitAccountNumber from test
Updated 8-Dec-22 2:36am

1 solution

Your query doesn't work with your example XML, because the DebitAccount node is not a direct child of the Request node.

Change your query to specify the correct path to the node:
select CAST(CAST(REQ.MQXMLdata AS XML).query('data(/*/Request/SettlementPostingRequest/DebitAccount/DebitAccountNo)') AS VARCHAR(50)) AS DebitAccountNumber from test 
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900