I have XML data stored in a SQL Server 2016 database that I need to query a date range in a stored procedure around the PROJECT_END
to return the application_ID
's
<PROJECTS xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<row>
<APPLICATION_ID>3012448</APPLICATION_ID>
<ACTIVITY>D23</ACTIVITY>
<ADMINISTERING_IC>NU</ADMINISTERING_IC>
<APPLICATION_TYPE>1</APPLICATION_TYPE>
<ARRA_FUNDED xsi:nil="true" />
<BUDGET_START>01/01/1985</BUDGET_START>
<BUDGET_END>12/31/1985</BUDGET_END>
<FOA_NUMBER xsi:nil="true" />
<PROJECT_START>01/01/1985</PROJECT_START>
<PROJECT_END>12/31/1987</PROJECT_END>
</row>
</PROJECTS>
I have the select part but can't seem to query based upon the date range using PROJECT_END
. Also would BETWEEN
work?
SELECT
nref.value('APPLICATION_ID[1]', 'Int') APPLICATION_ID
FROM
STAGING
CROSS APPLY
XMLData.nodes('/PROJECTS/row') AS r(nref)
WHERE
nref.value('PROJECT_END[1]', 'varchar(max)') > '1/1/1987'
AND nref.value('PROJECT_END[1]', 'varchar(max)') < '1/1/1990'
I have no control over the formatting of the date fields, unless I can convert it on the fly. Any help is appreciated.