S M Shamimul Hasan
S M Shamimul Hasan

Reputation: 6684

Finding Date Range from String Values in SPARQL

I have triples that organized like following.

<nc:person1> <nc:hasDate> "201701"
<nc:person2> <nc:hasDate> "201703"
<nc:person3> <nc:hasDate> "201705"
<nc:person4> <nc:hasDate> "201706"
<nc:person5> <nc:hasDate> "201606"

Here object part of the triple contains date information in string format. First, four digits of a date represent year and last two digits represent month. For example, in "201701", 2017 is the year and 01 means January month.

I need to write a SPARQL query to find all the dates that are in the reange of March 2017 to June 2017. My result should look like following.

"201703"
"201705"
"201706"

I think I need to write a SPARQL query like following:

SELECT ?date WHERE{
 ?person nc:hasdate ?date.FILTER(?)
}

I am not sure what filter condition I need to write. Could you please let me know how to parse a string as a date and find date range? Thank you in advance.

Upvotes: 0

Views: 377

Answers (1)

Sirko
Sirko

Reputation: 74086

So I tested it (just to be sure) and direct string comparison works out for the above example:

SELECT ?date
WHERE {
  ?person nc:hasDate ?date .
  FILTER( (?date > "201702") && (?date < "201707") )
}

Note, that this will only work for ISO-compliant date-strings. Other formats may fail.

Upvotes: 2

Related Questions