How do you perform date transformations in queries?

I'd like to query all individuals with a datatime property newer than say 6 months. The query would be something like:

SELECT ?s
WHERE {
    ?s <http://....#myProperty> ?ts
    FILTER(?ts > NOW() - "6 MONTHS INTERVAL")
}

but I cannot get the syntax of the FILTER right. How would you do that in Stardog?

I've seen that there is a function called swrlb:subtractDayTimeDurationFromTime in Stardog manual but I haven't seen any example of usage of it.

You can subtract the date from now() and compare it to a duration. Give this a try.

select ?s
where {
  ?s :myProperty ?ts
  filter(now() - xsd:datetime(?ts) < "P6M"^^xsd:duration)
}

I'm not quite sure why but I had to add the xsd:datetime cast even though the dummy data I was using to test the query was already a xsd:datetime.

I had written a custom function to do almost exactly what you had written originally. It would extract the date from a natural language string so you could say somethinglike

FILTER(?ts < myFunc:speakTime("6 months ago")

I'll have to dig it up and find out where I left off on that.

Thanks, it worked fine! I think that more info about datetimes manipulation is required in the documentation.

Glad to hear it. It looks like that function I wrote just needs to be updated for Stardog 7 and it's good to go. I wasn't able to deal with multiple dates so I punted and returned the first one found. I had thrown around the idea of making it a property function but I think I can now just have it return an array literal.

With that function your query would look like

select ?s
where {
  ?s :myProperty ?ts
  filter(?ts > util:fromSpokenTime("6 months ago"))
}

It reads a little oddly because it returns a date and it's really returning ?ts greater than [the time] "six months ago" so I'll have to fiddle around with how I can make it less confusing but it's nice to be able to work with dates this way sometimes. Let me know if it's something you're interested in or how you might like to have it work.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.