Nicholas Li
Nicholas Li

Reputation: 53

SPARQL How to get year as integer from xsd:date

I have a SPARQL query which choose people who has year of birth 1990. How could I get only year from xsd:date format? I can see how to get year from xsd:dateTime using year() but it doesn't work on xsd:date.

PREFIX dbr: <http://dbpedia.org/resource/>
PREFIX dbo: <http://dbpedia.org/ontology/>
SELECT ?birthYear
WHERE
{dbr:Ilse_DeLange dbo:birthDate ?birth
 #how to get year only as a integer?}

Upvotes: 5

Views: 4697

Answers (2)

AndyS
AndyS

Reputation: 16630

To do it without any assumptions on extensions for xsd:date needs a bit of string bashing.

You can use REPLACE on the lexical form of ?birthDate to get year as a string then cast it to xsd:integer.

REPLACE(str(?birthDate), "(\\d*)-.*", "$1")

Upvotes: 0

UninformedUser
UninformedUser

Reputation: 8465

According to the specs, year() works on xsd:dateTime literals only.

Depending on the triple store, you might try to convert to xsd:dateTime first:

PREFIX dbr: <http://dbpedia.org/resource/>
PREFIX dbo: <http://dbpedia.org/ontology/>
SELECT DISTINCT (year(xsd:dateTime(?birthDate)) as ?birthYear) WHERE {
  dbr:Ilse_DeLange dbo:birthDate ?birthDate
}

Upvotes: 3

Related Questions