Shredding XML Conditionally in SQL Server

Consider the following XML file: \[code\]<?xml version="1.0" encoding="ISO-8859-1"?><Input Control="1234567890"> <Patient> <AccountID>[email protected]</AccountID> <Name> <Title>Mr</Title> <First>John</First> <Middle>J</Middle> <Last>Smith</Last> </Name> <Addresses> <Address Type="Billing"> <Line1>123 Main St</Line1> <Line2>Unit #1</Line2> <City>Anytown</City> <State>MD</State> <PostalCode>78470</PostalCode> <Country>USA</Country> <Zone>TR89</Zone> <Address Type="Contact"> <Line1>55 Main St</Line1> <City>Anytown</City> <State>MD</State> <PostalCode>78470</PostalCode> <Country>USA</Country> <Zone>TR89</Zone> </Address> </Addresses> <Phones> <Phone Type="Daytime">555-221-2525</Phone> <Phone Type="Evening">555-355-1010</Phone> </Phones> <Selects> <Select Name="Current">0</Select> </Selects> </Patient></Input> \[/code\]I need to 'shred' the XML (using XPath/XQuery) into the following table: \[code\] Account Nam_Pr Nam_Fst Nam_Lst Adrs1Main Adrs2Main CityMain ... 1 Mr. John Smith 123 Main St Unit #1 Anytown\[/code\]The issue is, that I need to populate the AdrsMain fields with the data from the Address Type="Billing element only if it's present in the file. Otherwise, the AdrsMain fields are populated from the Address Type="Contact" element. How can I accomplish this?
 
Back
Top