13

I am using Jena's SPARQL engine and trying to write a query to filter on a date range as I need to find the value of a property after a fixed date.

My date property is in the following format:

 Fri May 23 10:20:13 IST 2014 

How do I write a SPARQL query to get other properties with dates greater than this?

RobV
  • 26,016
  • 10
  • 71
  • 114
cooljohny
  • 596
  • 3
  • 11
  • 29

1 Answers1

20

With your data in that format you can't filter on a range of it without adding a custom extension function to ARQ (which is intended for advanced users) since you would need to parse and interpret the date time string.

What you should instead be doing is translating your data into the standard date time format xsd:dateTime that all SPARQL implementations are required to support. See the XML Schema Part 2: Datatypes specification for details of this format.

Your specific example date would translate as follows:

2014-05-23T10:20:13+05:30

And you must ensure that you declare it to be a typed literal of type xsd:dateTime when you use it in data and queries. For example in the readable Turtle RDF syntax:

@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
@prefix : <http://example.org> .

:subject :date "2014-05-23T10:20:13+05:30"^^xsd:dateTime .

You could then write a SPARQL query that filters by range of dates like so:

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

SELECT *
WHERE
{
  ?s :date ?date .
  FILTER (?date > "2014-05-23T10:20:13+05:30"^^xsd:dateTime)
}

This finds all records where ?date is after the given date

RobV
  • 26,016
  • 10
  • 71
  • 114
  • and suppose I want to just apply range in filter with property of type string suppose age stored as string . then? – cooljohny Jun 05 '14 at 09:34
  • 1
    Comments are for clarifications on the specific question, if you have a separate question then you should ask it as such. Also read [Expressions and Testing Values](http://www.w3.org/TR/sparql11-query/#expressions) in the SPARQL specification if you haven't already – RobV Jun 05 '14 at 10:20
  • When I write the above query it gives error :Unresolved prefixed name: xsd:dateTime and when I dont specify it again gives error: Datatype format exception: "2014-06-08T14:26:39+0530"^^xsd:dateTime .What should I do? – cooljohny Jun 11 '14 at 10:56
  • For a start show your exact query string, trying to debug by guesswork is typically useless. However my guess would be that you haven't included the `PREFIX` declarations in your query – RobV Jun 11 '14 at 11:28
  • well I have include prefix but still this warning comes and output remains null . warning :WARN [main] (Log.java:78) - Datatype format exception: "2014-06-11T12:44:22+0530"^^xsd:dateTime – cooljohny Jun 11 '14 at 12:02
  • The format should be : 2014-05-23T10:20:13+05:30 ...not 2014-05-23T10:20:13+0530 – cooljohny Jun 12 '14 at 04:32
  • i've tested xsd:date and xsd:dateTime on protege and topbraid composer, none of them work? but integer works with filter why? – crapthings Jun 26 '18 at 05:14