0

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?

Boas Enkler
  • 12,264
  • 16
  • 69
  • 143

1 Answers1

4

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" .
    }
 }
Median Hilal
  • 1,483
  • 9
  • 17
  • 2
    Why do you use `BIND` and `FILTER` instead of the most obvious usage which is putting the literal in the object position of the triple pattern? – UninformedUser Jun 20 '17 at 13:57
  • You are right, I will modify it right away! I am used to work from Jena, I always need plugin points in the query. – Median Hilal Jun 20 '17 at 14:06