SPARQL Calculating the difference between two date
Asked Answered
C

2

13

What is the function to calculate difference between two SPARQL xsd:dateTime ?

I have found a reference to datediff, but this is not working. Does such a built in function exist in SPARQL as it does with most other query languages?

Christiachristian answered 3/9, 2013 at 22:4 Comment(3)
Did you ask this question earlier as an anonymous user and then delete it? I could have sworn I answered an almost identical question earlier today.Splotch
@JoshuaTaylor I did ask a question, but that was part of it and not exactly this question. The two questions are different and I didn't delete it.Christiachristian
Ok, just curious, because there was a question about subtraction of xsd:dates, and the fact that they produce a duration, and how to get the number of years instead. That's why I had the SPARQL code all ready to go. Just a strange coincidence, perhaps.Splotch
S
14

Some SPARQL engines may support date arithmetic directly. For instance, as mention in this answers.semanticweb.com question and answer, Jena supports date arithmetic, and you can subtract one date from another and get an xsd:Duration. Other implementations may support a datediff function. For instance, this Virtuoso example includes the use of bif:datediff. However, these are extensions and not guaranteed to be present in any particular SPARQL implementation.

For a more portable, though possibly less efficient solution, you can use the year to extract the year parts from a datetime. This lets you do, for instance,

select ?age where { 
  bind( "1799-12-14"^^<http://www.w3.org/2001/XMLSchema#date> as ?death )
  bind( "1732-02-22"^^<http://www.w3.org/2001/XMLSchema#date> as ?birth )
  bind( year(?death)-year(?birth) as ?age )
}

and get results

-------
| age |
=======
| 67  |
-------

This has the potential to be off by one, depending on the months and days of the two dates, but you can similarly work around this by using the month and day functions. This thread describes some of that sort of implementation.

Splotch answered 4/9, 2013 at 2:10 Comment(2)
@KimA.Jakobsen There a number of links, but I assume you mean the first one. The answers.semanticweb.com site is a bit flaky at times. I expect that it will be back up before too long. That said, the internet archive /wayback machine has a copy at web.archive.org/web/20131026044936/http://….Splotch
Ok, might have been a bit to fast, I home the site will get back up again soon. thanks for the archive page :)Cherish
K
9

I found this post when I was searching on how to calculate an age in SPARQL. Thanks to Joshua Taylor for giving the correct hint. However I improved the code segment by adding an adjustment so it is not off by one year if the person hasn't had their birthday in the year of their death. I thought it might be useful to some, who find this post when searching for the topic, as I did.:

select ?age where { 
  bind( "1799-12-14"^^<http://www.w3.org/2001/XMLSchema#date> as ?death )
  bind( "1732-02-22"^^<http://www.w3.org/2001/XMLSchema#date> as ?birth )
  bind( year(?death) - year(?birth) - if(month(?death)<month(?birth) || (month(?death)=month(?birth) && day(?death)<day(?birth)),1,0) as ?age )
}
Kindly answered 9/5, 2016 at 8:34 Comment(1)
Incredibly useful! Thanks for updating.Rum

© 2022 - 2024 — McMap. All rights reserved.