How to check if a string is null or empty in a SPARQL query?
Asked Answered
V

3

8

How do I check and filter out empty strings using sparql.

Vlaminck answered 25/5, 2014 at 20:7 Comment(4)
In the title, you ask about null or empty, but in the question you only mention empty strings. Are you asking about values that are "", or values that aren't bound? Also, what does this have to do with swrl? It doesn't appear to have anything to do with swrl, so I'm removing the tag, at least for now.Wrought
You are rigth about SWRL, thanks for the editing. And I mention about null or empty. Because I can always make my property empty ;)Vlaminck
Sorry, I'm still not clear what you mean. There are are a few problems that you could be having, and your question doesn't make it clear which you've got. Checking whether, e.g., a person has a foaf:name that's "" is a simple pattern select * where { ?x foaf:name "" }, but checking whether someone has no foaf:name requires select * where { ?x <>* ?x filter not exists { ?x foaf:name [] } }. if you've already got results and they include some bindings with undefined values, then you'd need something like filter bound(?name) or filter !bound(?name), depending on whether you wantWrought
to keep or exclude them. To make this question better, show us an example the query that you've got, the results that you're actually getting and the results that you'd like to be getting (which might be more or less than what you're actually getting, depending on what you're trying to achieve).Wrought
D
7

Try this:

BIND(BOUND(?abc) && strlen(?abc)>0 as ?check)
Duress answered 28/3, 2018 at 3:31 Comment(0)
D
2

to check a resource (?resource) with a property (?property) with an empty string is:

SELECT ?resource WHERE{  ?resource ?property "" }

I developed this answer using the information here and a good place to look to solution of semantics web is here.

Dyarchy answered 25/5, 2014 at 20:9 Comment(0)
A
1

If you want to check if a string is empty or NULL, you can create a new variable

SELECT ?s ?o ?len_o  
WHERE {?s prefix:predicate ?o  
        BIND(STRLEN(?o) AS len_o)  
      }

Then you can filter on the variable value

SELECT ?s ?o ?len_o  
WHERE {?s prefix:predicate ?o  
        BIND(STRLEN(?o) AS ?len_o)  
        FILTER (?len_o = 0)  
      }  

Or you can directly create a filter

SELECT ?s ?o  
WHERE {?s prefix:predicate ?o  
        FILTER (STRLEN(?o) != 0)  
      }  
Albertalberta answered 16/11, 2021 at 11:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.