Ivo Velitchkov
Ivo Velitchkov

Reputation: 2431

How to increment dates in SPARQL?

How can I increment numbers in general and dates in particular in SPARQL? Concretely, I have a filter to get me only people born today: FILTER (STRLEN(STR(?born)) > 6 && (SUBSTR(STR(?born),6)=SUBSTR(STR(bif:curdate('')),6)). How to extend this filter so I get also people born in next two days?

(I may accept/ignore or correct the use the private function)

UPDATE

As the proposed solution didn't work on DBpedia, I implemented it with Virtuoso private function but I have no idea, apart from using UNION, how to change the filter or find another elegant solution so that I get people born today, tomorrow and the day after tomorrow. Currently this part of the query looks like this:

BIND ((SUBSTR(STR(?born),6)=SUBSTR(STR(now()),6,5)) as ?tday)
BIND ((SUBSTR(STR(?born),6)=SUBSTR(STR(bif:dateadd("day", 1, now())),6,5)) as ?tday1)
BIND ((SUBSTR(STR(?born),6)=SUBSTR(STR(bif:dateadd("day", 2, now())),6,5)) as ?tday2)

FILTER (STRLEN(STR(?born)) > 6)
FILTER (?tday) .

Replacing ?tday with ?tday1 and ?tday2' works fine, but how all three dates in the result?

Upvotes: 0

Views: 1309

Answers (2)

Ivo Velitchkov
Ivo Velitchkov

Reputation: 2431

After some attempts, I found the solution that works fine for me. Here I'm taking all born today and in the next 7 days:

values ?d {0 1 2 3 4 5 6 7}

BIND ((SUBSTR(STR(?born),6) as ?bornSTR))
BIND ((SUBSTR(STR(bif:dateadd("day", ?d, now())),6,5)) as ?day)

FILTER (STRLEN(STR(?born)) > 6 &&  STR(?bornSTR) = STR(?day))

Upvotes: 0

Joshua Taylor
Joshua Taylor

Reputation: 85873

If your endpoint supports duration arithmetic, you can add a duration to the date. For instance, here we can add a duration of two days to a date and see the correct behavior where the end date wraps around to the next month:

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

select ?begin ?end where {
  values ?begin { "2005-02-28T00:00:00Z"^^xsd:dateTime }
  values ?duration { "P2DT0H0M0.000S"^^xsd:duration }
  bind( (?begin + ?duration) as ?end)
}
-----------------------------------------------------------------------------------
| begin                                | end                                      |
===================================================================================
| "2005-02-28T00:00:00Z"^^xsd:dateTime | "2005-03-02T00:00:00.000Z"^^xsd:dateTime |
-----------------------------------------------------------------------------------

Upvotes: 3

Related Questions