简体   繁体   中英

SQL xml query returns null

I have the following XML:

<Matter> 
  <CriticalDates>
    <CriticalDate>
      <CriticalDateId>2</CriticalDateId>
      <Name>Instruction Date</Name>     
      <Value_FieldId>9F21</Value_FieldId>
      <Confirmed_FieldId />
      <Status>In Progress</Status>
      <ConfirmStatus />
      <CompPercent>0</CompPercent>
      <Order>0</Order>
      <Value>2014-03-28T06:00:00+11:00</Value>
      <Confirmed>false</Confirmed>
    </CriticalDate>
    <CriticalDate>
      <CriticalDateId>-2</CriticalDateId>
      <Name>Completion Date</Name>
      <Value_FieldId>9F22</Value_FieldId>
      <Confirmed_FieldId>9F27</Confirmed_FieldId>
      <Status>Complete</Status>
      <ConfirmStatus />
      <CompPercent>0</CompPercent>
      <Order>1</Order>
      <Value />
      <Confirmed>false</Confirmed>
    </CriticalDate>
    <CriticalDate>
      <CriticalDateId>-3</CriticalDateId>
      <Name>Not Proceeding Date</Name>
      <Value_FieldId>9F23</Value_FieldId>
      <Confirmed_FieldId />
      <Status>Not Proceeding</Status>
      <ConfirmStatus />
      <CompPercent>0</CompPercent>
      <Order>2</Order>
      <Value />
      <Confirmed>false</Confirmed>
    </CriticalDate>
  </CriticalDates>
</Matter>

To select all the nodes as rows i'm using:

SELECT
  MatterId,
  MatterXml,
  MD.CD.value('(Name)[1]', 'VARCHAR(50)') AS 'Name', 
  MD.CD.value('(Status)[1]', 'VARCHAR(50)') AS 'Status', 
  MD.CD.value('(value)[1]', 'DATE') AS 'CriticalDate', 
  MD.CD.value('(Confirmed)[1]', 'VARCHAR(50)') AS 'Confirmed'
FROM
  dbo.Matter m 
  CROSS APPLY m.MatterXml.nodes('/Matter/CriticalDates/CriticalDate') AS MD(CD)

When i run this i get 3 rows back but all CriticalDates return as NULL even the first one when there is a date in the XML. Please help!

XML is case sensitive. Try is with:

MD.CD.value('(Value)[1]', 'DATE') AS 'CriticalDate', 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM