1

This seems very basic, but I haven't been able to find an example that works for me, so I'd appreciate any advice.

I have a SQL Server function that determines various dates based on our fiscal year and today's date, and returns one row which looks like... <row LastDayPrevMonth="2015-04-30T00:00:00" LastDayPrevMonthLY="2014-04-30T00:00:00" ... />

In the stored proc which calls that function, I've done...

DECLARE @X XML
SET @X = dbo.GetFiscalYearDates()

...but then I can't seem to extract the value of LastDayPrevMonth.

I've tried dozens of variations of this: SELECT ROW.ITEM.VALUE('LastDayPrevMonth', 'VARCHAR(30)')[1] AS Foo FROM @x.nodes('row/item') ... sometimes with an "AS Bar" at the end...

That particular syntax gives the error "incorrect syntax near the keywork 'as'", but any tweaks I do don't help.

Thanks for your assistance, dudes!

1 Answer 1

2
declare @doc xml

select @doc= '
<root>
<row LastDayPrevMonth="2015-04-30T00:00:00" LastDayPrevMonthLY="2014-04-30T00:00:00" />
</root>
'

SELECT 
      LastDayPrevMonth = Y.i.value('(@LastDayPrevMonth)[1]', 'datetime')
    , LastDayPrevMonthLY = Y.i.value('@LastDayPrevMonthLY[1]', 'datetime') 

FROM 
    @doc.nodes('root/row') AS Y(i)
Sign up to request clarification or add additional context in comments.

1 Comment

I appreciate it granadaCoder; I can't vote you up because I don't have 15 points earned. For future readers, the function which returns the XML has to have the clause on the end: ... FOR XML RAW, ROOT('root') to get the root node in there.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.