SPARQL Calculation of the difference between two dates

What is the function of calculating the difference between two SPARQL xsd: dateTime parameters?

I found a dated link, but this does not work. Is there such a built-in function in SPARQL, as in most other query languages?

+8
source share
2 answers

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

For a more portable, albeit less efficient, solution, you can use year to extract parts of the year from date and time. This allows you, for example,

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 the results

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

This can be disabled by one, depending on the months and days of two dates, but you can work around this in a similar way using month and day . This thread describes some of these implementations.

+9
source

I found this post when I was looking for how to calculate age in SPARQL. Thanks to Joshua Taylor for giving the right hint. However, I improved the code segment by adding an adjustment so that it does not shut off for a year if the person did not have his birthday in the year of their death. I thought this might be useful for some who find this post when searching for a topic, like me .:

 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 ) } 
+8
source

Source: https://habr.com/ru/post/953093/


All Articles