Given the following SQL:
drop table if exists #testXML
create table #testXML (InputXML xml)
insert into #testXML
values ('<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<document>
<table name="tableName1">
<column name="ID">000010313500011171011710001 </column>
<column name="StartDate">10/27/2019</column>
<column name="EndDate">11/02/2019</column>
</table>
</document>')
I'm trying to get output like this:
ID StartDate EndDate
000010313500011171011710001 10/27/2019 11/02/2019
Here's my start, but I'm just flailing at this point.
SELECT
px1.tbl.value('@name','nvarchar(50)') as TableName
,px2.col.value('@name','nvarchar(50)') as ColName
from #testXML px
cross apply inputxml.nodes ('/document/table') as px1(tbl)
cross apply inputxml.nodes ('/document/table/column') as px2(col)
This is on SQL Server 2017.
Your SQL needs to be adjusted as follows, by leveraging the @name
attribute value.
SQL
-- DDL and sample data population, start
DECLARE @tbl TABLE (InputXML xml)
INSERT INTO @tbl (InputXML)
VALUES ('<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<document>
<table name="tableName1">
<column name="ID">000010313500011171011710001</column>
<column name="StartDate">10/27/2019</column>
<column name="EndDate">11/02/2019</column>
</table>
</document>');
-- DDL and sample data population, end
SELECT col.value('(column[@name="ID"]/text())[1]','nvarchar(50)') as ID
, col.value('(column[@name="StartDate"]/text())[1]','DATE') as StartDate
, col.value('(column[@name="EndDate"]/text())[1]','DATE') as EndDate
FROM @tbl tbl
CROSS APPLY tbl.InputXML.nodes('/document/table') AS tab(col);
Output
+-----------------------------+------------+------------+
| ID | StartDate | EndDate |
+-----------------------------+------------+------------+
| 000010313500011171011710001 | 2019-10-27 | 2019-11-02 |
+-----------------------------+------------+------------+