1

I have an XML datatype and want to convert an element into a sql DateTime variable. How?

e.g.

Declare @Xml Xml
Set @Xml = '<Root><DateFrom>2008-10-31T00:00:00</DateFrom></Root>'

Declare @DateFrom DateTime
Set @DateFrom = ?????

How can I set @DateFrom with the date from the xml above?

1 Answer 1

1
Declare @Xml Xml
Set @Xml = '<Root><DateFrom>8/10/2008</DateFrom></Root>'

Declare @DateFrom DateTime
Select @DateFrom= t.b.value('DateFrom[1]', 'datetime')
from @xml.nodes('//Root') t(b)

I highly recommend you look at the .nodes functionality of the XML data type. The above code pulls out the value of the DateFrom element and converts it to Date Time format.

Sign up to request clarification or add additional context in comments.

Comments

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.