Boas Enkler
Boas Enkler

Reputation: 12557

Query Wikidata by GeoNames ID

I try to query Wikidata for a city by its GeoNames ID.

I can see the id of the property is P1556: https://www.wikidata.org/wiki/Property:P1566

So my query for Berlin (as an example) is:

SELECT * WHERE {
   ?id wdt:P1566 wd:2950157
   }

But I don't get a match.

What am I doing wrong?

Upvotes: 0

Views: 725

Answers (1)

Median Hilal
Median Hilal

Reputation: 1531

A simple trial and error on Wikidata results in the following: No need for wd:2950157, it is a literal, so use only "2950157" instead. Use the following (SERVICE is used to show the name of the city):

Edit: The straightforward method:

SELECT ?id ?idLabel WHERE {      
  ?id wdt:P1566 "2950157".      
  SERVICE wikibase:label {
      bd:serviceParam wikibase:language "en" .
  }
}

End of EDIT

Using BIND or FILTER

SELECT ?id ?idLabel WHERE {
  Bind ("2950157" as ?x)
  ?id wdt:P1566 ?x.      
  SERVICE wikibase:label {
      bd:serviceParam wikibase:language "en" .
  }
}

Or (a slower option) use FILTER

 SELECT ?id ?idLabel WHERE {       
    ?id wdt:P1566 ?x.
    filter (?x = "2950157")
    SERVICE wikibase:label {
        bd:serviceParam wikibase:language "en" .
    }
 }

Upvotes: 4

Related Questions