Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Filter by date range in SPARQL

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?

like image 739
cooljohny Avatar asked Jun 05 '14 04:06

cooljohny


People also ask

What types of queries does SPARQL support?

SPARQL allows for a query to consist of triple patterns, conjunctions, disjunctions, and optional patterns. Implementations for multiple programming languages exist. There exist tools that allow one to connect and semi-automatically construct a SPARQL query for a SPARQL endpoint, for example ViziQuer.

What is construct query in SPARQL?

The CONSTRUCT query form returns an RDF graph. The graph is built based on a template which is used to generate RDF triples based on the results of matching the graph pattern of the query.

What is optional SPARQL?

OPTIONAL is a binary operator that combines two graph patterns. The optional pattern is any group pattern and may involve any SPARQL pattern types. If the group matches, the solution is extended, if not, the original solution is given (q-opt3. rq).

What is SPARQL prefix?

"PREFIX", however (without the "@"), is the SPARQL instruction for a declaration of a namespace prefix. It allows you to write prefixed names in queries instead of having to use full URIs everywhere. So it's a syntax convenience mechanism for shorter, easier to read (and write) queries.


1 Answers

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

like image 176
RobV Avatar answered Sep 20 '22 14:09

RobV