ValQc
ValQc

Reputation: 77

How to compare date values (xsd:date) with years in SPARQL?

I created a SPARQL query to compare if the year of dbo:birthDate is different from the value of dbo:birthYear:

PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX dbo: <http://dbpedia.org/ontology/>
prefix xsd: <http://www.w3.org/2001/XMLSchema#>
prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#>  

Select distinct ?s ?vv1 ?vv2 where
  { ?s a dbo:Person ; dbo:birthDate ?v1 .
    ?s dbo:birthYear ?v2 . 
    bind(year(xsd:date(?v1)) as ?vv1) .
    bind(xsd:gYear(?v2) as ?vv2) .
    filter(?vv1 !=  ?vv2)
  }

I got the following answer:

                S                  vv1    vv2
<http://example.com/resource/1>   1993   1993
<http://example.com/resource/5>   1998   1993   
<http://example.com/resource/7>   1978   1978   
<http://example.com/resource/13>  1993   1993 

The right answer is only the resource 5, that has the year of birthDate equal to 1998 and the year of birthYear equal to 1993. The answer shows that vv1 and vv2 have values to years without " ", and apparently both have the same type. So, I do not understand why SPARQL said that vv1 and vv2 in record 1, for example, are distinct. I looked for how I can compare dates in SPARQL and I think my query is correct.

I tested this query using Fuseki 2.3.1

Examples of my instances:

@prefix dbo:   <http://dbpedia.org/ontology/> .
@prefix :      <http://example.com/resource/> .
@prefix dbp:   <http://dbpedia.org/property/> .
@prefix owl:   <http://www.w3.org/2002/07/owl#> .
@prefix rdf:   <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix xml:   <http://www.w3.org/XML/1998/namespace> .
@prefix xsd:   <http://www.w3.org/2001/XMLSchema#> .
@prefix mu:    <http://example.com/resource/> .
@prefix rdfs:  <http://www.w3.org/2000/01/rdf-schema#> .
@prefix foaf:  <http://xmlns.com/foaf/0.1/> .

mu:1    a                dbo:Actor , 
                         dbo:Person , 
                         dbo:MusicalArtist , 
                         mu:MusicActor , 
                         owl:NamedIndividual ;
        dbo:birthDate    "1993-03-11"^^xsd:date ;
        dbo:birthYear    "1993" ;
        foaf:name        "John Joe"@en , 
                         "Joe, John"@en .

mu:5    a                <http://example.com/MusicActor> , 
                         dbo:Actor , 
                         dbo:Person , 
                         mu:MusicActor , 
                         owl:NamedIndividual ;
        dbo:birthDate    "1998-04-11" , 
                         "1998-04-11"^^xsd:date ;
        dbo:birthYear    "1993" ;
        foaf:name        "Barry Hannah"@en , 
                         "Barry B. Hannah"@en .

mu:7    a                dbo:Person , 
                         dbo:MusicalArtist , 
                         owl:NamedIndividual ;
        dbo:birthDate    "1978-05-11" , 
                         "1978-05-11"@en ;
        dbo:birthYear    "1978" ;
        foaf:name        "Helen Petty" , 
                         "Helen Petty"@en .

Anybody can, please, say why this is happing? I unfortunately have no idea. Any help will very appreciated!

Upvotes: 4

Views: 5010

Answers (1)

scotthenninger
scotthenninger

Reputation: 4001

The SPARQL year() function returns an integer, so you need to cast ?v2 to an integer:

SPARQL DISTINCT ?s ?vv1 ?vv2
WHERE {
   ?s a dbo:Person ; dbo:birthDate ?v1 .
   ?s dbo:birthYear ?v2 . 
   BIND(year(xsd:date(?v1)) as ?vv1) .
   BIND(xsd:integer(?v2) as ?vv2) .
   FILTER(?vv1 != ?vv2)
}

Upvotes: 5

Related Questions