Filter in Sparql query not working

rdf, sparql

Solution

The problem is probably in the date comparison, assuming that it's stored as an `xsd:date` typed literal in the original data. You cannot compare strings to dates without type conversion.

Some things to try:

?cleanUpDate < "2012-04-11"^^xsd:date    # Compare against date
STR(?cleanUpDate) < "2012-04-11"         # Convert to string before comparison

Also, depending on the structure of the data, you might need two separate `OPTIONAL` blocks for the `?cleanUpStatus` and `?cleanUpDate`.

Problem

I am running a sparql query against one of my sparql end point that supports only Sparql 1.0. I am trying to get a list of users from store using following query : ``` PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> PREFIX tmp: <http://example.com/schema/temp#> PREFIX resource: <http://example.com/2010/record/schema#> Describe ?userURI WHERE { ?userURI rdf:type resource:User. OPTIONAL { ?userURI tmp:dataCleanupStatus ?cleanUpStatus. ?userURI tmp:lastDataCleanupDate ?cleanUpDate. } FILTER ( (!bound(?cleanUpStatus) || ?cleanUpStatus !="Running") ) FILTER( (!bound(?cleanUpDate) || ?cleanUpDate < "2012-04-11" ) ) } ``` With the above query I am trying to get users, where : - Either clean up status triple does not exists, or, status is not "Running" - clearnUpDate triple either does not exists or, is less than the specified date. it's not returning the record it should be returning. You might say I should use xsd functions, but, they are only supported in Sparql 1.1 Please advice. Modified Query: ``` PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> PREFIX tmp: <http://example.com/schema/temp#> PREFIX resource: <http://example.com/2010/record/schema#> Describe ?userURI WHERE { ?userURI rdf:type resource:User. OPTIONAL { ?userURI tmp:dataCleanupStatus ?cleanUpStatus. } OPTIONAL { ?userURI tmp:lastDataCleanupDate ?cleanUpDate. } FILTER ( !bound(?cleanUpStatus) || ?cleanUpStatus !="Running" ) FILTER ( !bound(?cleanUpDate) || ?cleanUpDate < "2012-04-11" ) } ```

Original source