[SPARQL][WIKIDATA] STR() : when comparing values

 STR()

How did I encounter this?

FILTER(?placeName = "worldwide")
When trying to compare values using =, it didn't work, even though it was the case it should be equal. The solution was to apply STR().
FILTER(STR(?placeName) = "worldwide")
It was due to data type mismatches in SPARQL.

Explanation

SPARQL Literals Have Implicit Datatypes. In SPARQL, literals (like "worldwide") have hidden data types attached. The ?placeName values retrieved from Wikidata are not just plain strings, but typed RDF literals.

For example:

?placeName = "worldwide"@en
?placeName = "worldwide"^^xsd:string
?placeName = "worldwide" (without explicit datatype)


FILTER(?placeName = "worldwide")

SPARQL strictly compares values fails because "worldwide" (plain string) is not considered identical to "worldwide"@en (language-tagged string) or "worldwide"^^xsd:string.

Applying STR()

FILTER(STR(?placeName) = "worldwide")

By wrapping ?placeName in STR(), it forces SPARQL to convert the value to a plain string, removing any data type or language tag.

FILTER(STR(?placeName) = "worldwide")
STR(?placeName) extracts just "worldwide" from "worldwide"@en, "worldwide"^^xsd:string, or any other RDF literal.



Comments