Search code examples
sparqlgraphdb

Difference in days between two dates in GraphDB


I need some help to calculate the difference between two dates with SPARQL in Ontotext GraphDB. I know that SPARQL protocol does not support arithmetic operation on dates, however some SPARQL engines support it.

Just as an example in Fuseki I could do .

PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>

SELECT ?duration
WHERE {
  BIND (("2011-02-04T14:45:13.815-05:00"^^xsd:dateTime - "2011-02- 
  02T14:45:13.815-05:00"^^xsd:dateTime) AS ?duration)
}

the result is duration: "P2DT0H0M0.000S"^^xsd:duration. Then I can get 2 days diff, or Virtuoso provides a built-in function bif:datediff.

My question is, if is there something similar on GraphDB to solve this easy problem without a big workaround.

Thanks in advance.


Solution

  • That was solved in 8.7.0 (released 28 September):

    GDB-2887 As a GraphDB user I need support of “+” / “-” operations between xsd:dateTime and xsd:duration with SPARQL

    Now your query should return "P2DT0H0M0.000S"^^xsd:dateTimeDuration.


    Alternatively, one could use federated queries to public endpoints based on Virtuoso, Blazegraph, Fuseki etc.:

    PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
    PREFIX bif: <bif:>
    SELECT ?duration1 ?duration2 ?duration3 ?duration4 { 
        VALUES (?start ?end)
            {("2011-02-02T14:45:14"^^xsd:dateTime "2011-02-04T14:45:13"^^xsd:dateTime)}
        SERVICE <http://dbpedia.org/sparql>           #-- Virtuoso
            { BIND ((?end - ?start)/86400.0 AS ?duration1) }
        SERVICE <http://dbpedia.org/sparql>           #-- Virtuoso
            { BIND (bif:datediff("day", ?start, ?end) AS ?duration2) }
        SERVICE <https://query.wikidata.org/sparql>   #-- Blazegraph
            { BIND ((?end - ?start) AS ?duration3) }
        SERVICE <http://zbw.eu/beta/sparql/stw/query> #-- Fuseki
            { BIND (day(?end - ?start) AS ?duration4) }
    }
    

    Try on FactForge!