0

I'm getting xml in SQL Server, and I need to get businessDate value

I tried this

Select C.value('(businessDate)[1]', 'DATETIME') AS 
FROM @input.nodes('/MealsUsed/*') AS T(C)

xml:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<MealsUsed businessDate="10/09/2013" count="10" institutionID="Test">
<MealUsed>
    <TransactionDate>02/05/2013 08:10:06</TransactionDate>
    <LocationID>8</LocationID>
    <Swipes>1</Swipes>
</MealUsed>
<MealUsed>
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Gary
  • 9
  • 1

1 Answers1

1

businessDate is attribute, not element, so you have to use it with @:

select
    C.value('@businessDate', 'datetime') as businessDate
from @input.nodes('MealsUsed') as T(C)

Actually, since businessDate is not in xml-date format, i'd be better to get it to varchar and then convert with appropriate format:

select
    convert(datetime, C.value('@businessDate', 'varchar(10)'), 103) as businessDate
from @input.nodes('MealsUsed') as T(C)

sql fiddle demo

Roman Pekar
  • 107,110
  • 28
  • 195
  • 197