Onkeltem
Onkeltem

Reputation: 2438

How to filter objects by property on Wikidata using SPARQL?

I cannot find a way to filter objects by name. For example, this query should limit country objects to those with official name "Canada":

SELECT DISTINCT ?country ?official_name WHERE {
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en,fr". }
  ?country wdt:P31 wd:Q6256.
  ?country wdt:P1448 ?official_name.
  ?country wdt:P1448 "Canada".
}
LIMIT 100

Here is a direct link to the query.

Any ideas?

Upvotes: 2

Views: 745

Answers (1)

TallTed
TallTed

Reputation: 9434

Knowing how these things go, you'll likely be happier in the end with this somewhat fuzzy filter, than an exact match test... but you could change filter ( contains (str(?official_name), "Canada") ) below to filter ( str(?official_name) = "Canada")

SELECT DISTINCT ?country ?official_name WHERE {
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en,fr". }
  ?country wdt:P31 wd:Q6256.
  ?country wdt:P1448 ?official_name.
# ?country wdt:P1448 "Canada".
  filter ( contains (str(?official_name), "Canada") )
}
LIMIT 100

Upvotes: 3

Related Questions