0

I have a XML file where i have a date child with this format: 1958-07-11+01:00

Currently I import this as plain text to my SQL Server, but I would like for my SQL database to understand this, so I can select only the last 7 days for my search. How do I convert this to a time-format my SQL Server understands and how do I then output only the rows with a change date in the last 7 days? Thanks!

2
  • What database are you using? Commented Jan 12, 2016 at 13:27
  • Have you tried anything yet? Commented Jan 12, 2016 at 13:31

2 Answers 2

1

If you can influence the generation of the XML you have to deal with, I'd suggest to write your dates in a proper way into the XML (ISO8601).

But nevertheless you can go on with what you have:

declare @xml XML=
'<root>
    <node yourdate="1958-07-11+01:00">Some value for 1958-07-11</node>
</root>';

SELECT @xml.value('(/root/node/@yourdate)[1]','datetime') AS YourDate --Look at the time shift!

--and now, how to filter this
declare @xmlMany XML=
'<root>
    <node yourdate="1958-07-11+01:00">Some value for 1958-07-11</node>
    <node yourdate="1999-07-23+01:00">Some value for 1999-07-11</node>
    <node yourdate="2016-01-13+01:00">Some value for 2016-01-13</node>
    <node yourdate="2016-01-12+01:00">Some value for 2016-01-12</node>
</root>';

WITH AllNodes AS
(
    SELECT One.Node.value('@yourdate','datetime') AS YourDate
          ,One.Node.value('.','varchar(max)') AS NodeValue
    FROM @xmlMany.nodes('/root/node') AS One(Node)
)
SELECT * 
FROM AllNodes
WHERE YourDate> GETDATE()-7
Sign up to request clarification or add additional context in comments.

3 Comments

I guess @yourdate was meant to be a declared value?
@ArvoBowen no, @yourdate is addressing the xml's attribute in a XPath.
@ArvoBowen happy new year 😉
0

You should save this value as a datetime. This is more convenient and according to conventions. Do the next steps:

  1. Replace your varchar column with datetime.

  2. Convert all the values that you have to save it as the new type.

    convert(datetime, 'Oct 23 2016 11:02:44:013AM', 109)

or

`convert(datetime, "your string datetime value drew from xml file", 109).`

This will convert your string date value into an sql datetime value.

  1. Select your 7 last days values as it is required.

    select * from table where convert(datetime, date, 101) between (Getdate() - 6) and Getdate() order by date

  2. In the future you should convert your values to datetime before saving.

Another way is to parse the varchar value, convert the values to the integer type and order this using required conditions. But I think this is not good approach.

2 Comments

Little thing - datetime is now a legacy data type. For new development, datetime2 is the preferred data type. blogs.msdn.com/b/cdnsoldevs/archive/2011/06/22/…
It is always a good advise to store data in a proper format... But this is not an answer to the OP's question. The point is - as far as I understand - to filter an incoming XML to import this into existing tables. We don't know, where the XML comes from and we don't know where it is written to...

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.