SPARQL date range - sparql

I'm trying to obtain all records between certain dates. The date field has appears in this format: 2012-01-31. I think it is of type: <http://www.w3.org/2001/XMLSchema#date>
How would I modify the query below to extract records with date greater than 2012-01-31 please?
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX owl: <http://www.w3.org/2002/07/owl#>
PREFIX lrppi: <http://landregistry.data.gov.uk/def/ppi/>
PREFIX skos: <http://www.w3.org/2004/02/skos/core#>
PREFIX lrcommon: <http://landregistry.data.gov.uk/def/common/>
SELECT ?county ?postcode ?amount ?date
WHERE
{
?transx lrppi:pricePaid ?amount .
?transx lrppi:transactionDate ?date .
?transx lrppi:propertyAddress ?addr.
?addr lrcommon:postcode "PL6 8RU"^^xsd:string .
?addr lrcommon:postcode ?postcode .
# Cant get this line to work
# ?date lrppi:transactionDate ?date . FILTER ( ?date >= "1327968000"^^xsd:date )
OPTIONAL {?addr lrcommon:county ?county .}
}
ORDER BY ?postcode
If you want to play with this, you can enter your query here:
http://landregistry.data.gov.uk/landregistry/sparql/sparql.html

This is what the FILTER clause is designed for.
The Expressions and Testing Values section of the SPARQL specification covers this in detail, pretty much the first example in that section covers filtering on dates.
Edit
If you are new to SPARQL then I would recommend you read a good SPARQL tutorial like SPARQL by Example which is written by one of the specification authors. This will walk you through various parts of SPARQL and should help you get your head around the RDF data model and query language better.
In terms of dates they are represented using XML schema datatypes, for example expressing today as a date would be the following:
"2013-03-22"^^xsd:date
The linked specification covers the lexical form of various datatypes.
So for your example it would be the following:
FILTER ( ?date >= "2012-01-31"^^xsd:date )
If you are starting from a UNIX timestamp and trying to get to an xsd:date see Generating an xsd:dateTime in shell script which may provide a useful starting point.

Related

Matching two words together in sparql

How to List the laureate awards (given by their label) for which the description of the contribution (given by nobel:motivation) contains the word "human" together with the word "peace" (i.e., both words must be there).
I have use the bds:search namespace from the the full-text search feature of Blazegraph.
After visiting this link i have composed this query
Free text search in sparql when you have multiword and scaping character
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX bds: <http://www.bigdata.com/rdf/search#>
PREFIX nobel: <http://data.nobelprize.org/terms/>
SELECT ?awards ?description
WHERE {
?entity rdfs:label ?awards .
?entity nobel:motivation ?description .
FILTER ( bds:search ( ?description, '"human" AND "peace"' ) )
}
This query is returning me the following error on execution shown in image.
Error Image
How to correct this query and get the desired result?
You may take a look at the specification of this dataset or download an RDF dump of the dataset
Use bds:search to search for "human" category.Then apply filter and contain function to "peace".
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX bds: <http://www.bigdata.com/rdf/search#>
PREFIX nobel: <http://data.nobelprize.org/terms/>
PREFIX bif: <http://www.openlinksw.com/schemas/bif#>
SELECT ?awards ?description
WHERE {
?entity rdfs:label ?awards .
?entity nobel:motivation ?description .
?description bds:search "human" .
FILTER (CONTAINS(?description, "peace"))
}

SPARQL query returns multiple birth dates for same person

I am learning SPARQL and dbpedia by working through the queries in https://www.joe0.com/2014/09/22/how-to-use-sparql-to-query-dbpedia-and-freebase/ . I am testing a query to return John Lennon's date of birth and I am running my queries in http://dbpedia.org/sparql . The query is:
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX dbpedia-owl: <http://dbpedia.org/ontology/>
SELECT DISTINCT ?x0 ?x1 WHERE {
?x0 rdf:type foaf:Person.
?x0 rdfs:label "John Lennon"#en.
?x0 dbpedia-owl:birthDate ?x1.
}
It returns two rows containing the same date (9 Oct 1940). My question is: why does the query return two rows even though it uses DISTINCT? Prior to asking this question I checked the following:
Why does my SPARQL query duplicate results?
Duplicate rows when making SPARQL queries
but I don't think they explain the duplicate dates.
Edit: I converted the results to text and pasted them below
-------------------------------------- -----------------------------------------------------
x0 x1
--------------------------------------- -----------------------------------------------------
http://dbpedia.org/resource/John_Lennon 1940-10-09
http://dbpedia.org/resource/John_Lennon "1940-10-9"^^<http://www.w3.org/2001/XMLSchema#date>
As stated it seems dbpedia actually has two dates, 1940-10-09 (valid) and 1940-10-9 (invalid). The answer is to add a FILTER that converts the date to a string and only allows dates conforming to YYYY-MM-DD. Anyway it works!
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX dbpedia-owl: <http://dbpedia.org/ontology/>
SELECT DISTINCT ?x0 ?x1 STR(?x1) WHERE {
?x0 rdf:type foaf:Person.
?x0 rdfs:label "John Lennon"#en.
?x0 dbpedia-owl:birthDate ?x1.
FILTER (REGEX(STR(?x1),"[0-9]{4}-[0-9]{2}-[0-9]{2}")).
}
Well, it is not your fault! Simply the resource has both of these triples as you can see here. There are duplicates in the data.
I ran your query on the DBpedia endpoint and asked for the results in an RDF-based format (Turtle), and found that the lexical forms of the date literals are actually different:
"1940-10-09"^^xsd:date
"1940-10-9"^^xsd:date
The second isn't actually a legal xsd:date. The first is, which is probably why the SPARQL endpoint prints it in "pretty" fashion in the HTML table (as just 1940-10-09).
The result is a slowdown on queries because each access to an invalid date trig an exception (for example, with a query from fuseki) or the filter do the job to eliminate the wrong date, but it's costly

Getting all wikipedia articles with precise time

Some wikipedia articles has precise timestamp in infobox, like this one:
https://en.wikipedia.org/wiki/Apollo_11
(Launch date: July 16, 1969, 13:32:00 UTC)
or:
https://en.wikipedia.org/wiki/Remembrance_Day_bombing
(Date: 8 November 1987 10:43 (GMT))
Is there a way to get a list of all articles like this? Seems like it possible with SPARQL
AFAIK It would be possible, but it require to know what wiki property is linked to the date ( or date time ) field of the infobox; let me explain with an example:
PREFIX : <http://dbpedia.org/resource/>
PREFIX time-of-spacecraft-launch: <http://www.wikidata.org/entity/P619c>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
SELECT ?entity_label, ?property_label, ?time_of_spacecraft_launch WHERE {
:Apollo_11 owl:sameAs ?wikidata_entity .
?wikidata_entity time-of-spacecraft-launch: ?time_of_spacecraft_launch .
?wikidata_entity rdfs:label ?entity_label .
?wke_prop ?property_rel time-of-spacecraft-launch:.
?wke_prop rdfs:label ?property_label .
FILTER (LANG(?property_label)='en' && LANG(?entity_label)='it')
}
click here to se the result
Now we can gel all the article with the same kind of information simply by removing the where condition on Apollo_11:
PREFIX : <http://dbpedia.org/resource/>
PREFIX time-of-spacecraft-launch: <http://www.wikidata.org/entity/P619c>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
SELECT ?entity_label, ?property_label, ?time_of_spacecraft_launch WHERE {
?wikidata_entity time-of-spacecraft-launch: ?time_of_spacecraft_launch .
?wikidata_entity rdfs:label ?entity_label .
?wke_prop ?property_rel time-of-spacecraft-launch:.
?wke_prop rdfs:label ?property_label .
FILTER (LANG(?property_label)='en' && LANG(?entity_label)='it')
}
see the result herefy
in some cases may be useful to simplify the query:
PREFIX : <http://dbpedia.org/resource/>
PREFIX time-of-spacecraft-launch: <http://www.wikidata.org/entity/P619c>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
SELECT * WHERE {
?wikidata_entity time-of-spacecraft-launch: ?time_of_spacecraft_launch .
?wikidata_entity rdfs:label ?entity_label .
FILTER (LANG(?entity_label)='en')
}
ORDER BY DESC(?time_of_spacecraft_launch)
see the result here

Wikidata Query Service - Bug on the 'Whose birthday is today?' SPARQL Query Examples

I was playing with the SPARQL query examples. The query 'Whose birthday is today' always ends up in a QueryTimeoutException (Query deadline is expired).
However If I change the birthdate property P569 to the death date property P570, the query runs without throwing an exception.
Is there something to do to have the birth query successfully executed?
I think the query is possibly too heavy for the wikibase service. If you just try to get all the properties through the person's properties, you will have success. I tried the following:
PREFIX wikibase: <http://wikiba.se/ontology#>
PREFIX wdt: <http://www.wikidata.org/prop/direct/>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
SELECT distinct ?name (year(?date) as ?year) WHERE {
?entityS wdt:P569 ?date .
?entityS wdt:P1477 ?name.
FILTER (datatype(?date) = xsd:dateTime)
FILTER (month(?date) = month(now()))
FILTER (day(?date) = day(now()))
}

Protege Equivalent to query

Please help to list all Mealcourse from the wine Ontology or happy to receive useful links like this one :Sparql query on restriction list (Equivalent To) in protégé
MealCourse
and (hasFood value Pizza)
and(hasDrink value Wine)
Thank you
This is a bit complicated, but if you look into the ontology everything that is defined as a :MealCourse is an owl:equivalentClass, so you need to first find all owl:equivalentClass and then if you look into the result you will see that they are made of owl:intersectionOf parts. Then you need to break this intersection and filter so that you will only get objects that have :MealCourse as part of the intersection.
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX owl: <http://www.w3.org/2002/07/owl#>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX : <http://www.w3.org/TR/2003/PR-owl-guide-20031209/food#>
SELECT distinct *
WHERE { ?subject owl:equivalentClass ?object.
?object (owl:intersectionOf | owl:unionOf) ?node.
?node rdf:rest*/rdf:first ?eq.
Filter ( ?eq in (:MealCourse ) )
}
orderBy ?subject