0

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.

Beautiful Mind
  • 5,828
  • 4
  • 23
  • 42
  • 1
    I don't think you need to parse it. For ISO-formatted date-strings, direct comparison should suffice. – Sirko Nov 19 '17 at 18:05
  • Thanks for your reply. Could you please let me know how to write filter condition? – Beautiful Mind Nov 19 '17 at 18:17
  • 1
    I don't know why you don't use proper date literals, but lexicographical comparision might work for date strings of your type: `FILTER(?date >= "201703" && ?date <= "201706")` – UninformedUser Nov 19 '17 at 19:06

1 Answers1

2

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.

Sirko
  • 72,589
  • 19
  • 149
  • 183