1

SQL Server 2012

How to access "datefrom" value from below XML? Have try almost everything but without success :(

<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://www.w3.org/2003/05/soap-envelope">
   <SOAP-ENV:Body>
      <ns0:getInterestAndExchangeRatesResponse xmlns:ns0="http://swea.riksbank.se/xsd">
         <return>
            <datefrom>2020-03-05</datefrom>
            <dateto>2020-03-05</dateto>
         </return>
      </ns0:getInterestAndExchangeRatesResponse>
   </SOAP-ENV:Body>
</SOAP-ENV:Envelope>
user3345547
  • 717
  • 2
  • 6
  • 16

1 Answers1

3

You need to respect the XML namespaces in play - but once you do, this should work for you:

DECLARE @Data XML = '....(your XML here).....';

-- define the two XML namespaces in play
WITH XMLNAMESPACES('http://www.w3.org/2003/05/soap-envelope' AS soap,
                   'http://swea.riksbank.se/xsd'AS ns)
SELECT
    @Data.value('(soap:Envelope/soap:Body/ns:getInterestAndExchangeRatesResponse/return/datefrom)[1]', 'varchar(20)')

This will return something like:

2020-03-05
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459