Search code examples
sparqldbpedia

SPARQL replace empty attributes with others


I made a SPARQL query using the official DBpedia endpoint.

I'll take it for example for the main Question:

SELECT  ?museum ?EnglishAbstract   ?RussianAbstract 
WHERE { 

?museum dbpedia-owl:abstract ?EnglishAbstract. 
?museum a dbpedia-owl:Museum.
filter(lang(?EnglishAbstract)='en')

optional{
    ?museum dbpedia-owl:abstract ?RussianAbstract.
    ?museum a dbpedia-owl:Museum.
filter(lang(?RussianAbstract)='ru')
}}
GROUP BY ?museum

With this query (that works good), I find for every row (museum) a bunch of Abstract in 2 languages: English and Russian. The Russian abstracts are present only if available because I use "OPTIONAL". Obviously I get a lot of empty attributes.

I would replace the blank attribute with the English abstract (always present).

Reading the W3C SPARQL page, I found that there is a particular testing value that is true when a variable is set: Bound

I would like to write something like this:

If Bound (?RussianAbstract), "?RussianAbstract", "?EnglishAbstract"

--> if RussianAbstract is present, hold it; else, put the EnglishAbstract instead of it.

Does anyone know how can I make it work?


Solution

  • One way to do this is to use COALESCE:

    SELECT  ?museum (COALESCE(?RussianAbstract, ?EnglishAbstract) as ?Abstract)