I have a SQL server database with some data fields that have XML formatted data in the field and I want to export this to a new table or view with the XML data into columns...
Table Name: MyHistory
Field Names: AccountNo, Allergies
Example of data in field XML Field (Allergies):
Sorry, I am not following what you need exactly... I already have the database with the two fields and the data in the fields, I am just trying to take that data and parse it from the XML to fields
yes but if you want us to help you, you have to provide us sample data. that way you get faster answer. otherwise I have to create the sample data for you because I do not have remote access to your database.
create table #Chimera(AccountNo int, Allergies xml)
insert into #Chimera
select 66914,'<grdAllergies><Row><Allergy>celebrex, ceftin</Allergy><ID></ID><NDCIDs></NDCIDs><RxNorm></RxNorm><GroupID></GroupID><Reaction></Reaction><OnsetDate></OnsetDate><Status></Status></Row></grdAllergies>' union
select 54556,'<grdAllergies><Row><Allergy>Ibuprofin</Allergy><ID></ID><NDCIDs></NDCIDs><RxNorm></RxNorm><GroupID></GroupID><Reaction></Reaction><OnsetDate></OnsetDate><Status></Status></Row></grdAllergies>' union
select 15688,'<grdAllergies><Row><Allergy>Vitamin C</Allergy><ID></ID><NDCIDs></NDCIDs><RxNorm></RxNorm><GroupID></GroupID><Reaction></Reaction><OnsetDate></OnsetDate><Status></Status></Row></grdAllergies>' union
select 45684,'<grdAllergies><Row><Allergy>Penacillan</Allergy><ID></ID><NDCIDs></NDCIDs><RxNorm></RxNorm><GroupID></GroupID><Reaction></Reaction><OnsetDate></OnsetDate><Status></Status></Row></grdAllergies>' union
select 45881,'<grdAllergies><Row><Allergy>Celebrex</Allergy><ID></ID><NDCIDs></NDCIDs><RxNorm></RxNorm><GroupID></GroupID><Reaction></Reaction><OnsetDate></OnsetDate><Status></Status></Row></grdAllergies>'
select AccountNo,
Allergies.value('(//*[local-name()="Allergy"])[1]', 'nvarchar(max)') as Allergy,
Allergies.value('(//*[local-name()="Reaction"])[1]', 'nvarchar(max)') as Reaction,
case Allergies.value('(//*[local-name()="OnsetDate"])[1]', 'datetime')
when '1900-01-01 00:00:00.000' then null
else Allergies.value('(//*[local-name()="OnsetDate"])[1]', 'datetime')
end OnsetDate
From #Chimera
drop table #Chimera