QGIS – Creating Empty Output When Condition is False in If-Statement

expressionif statementqgis

When using if-conditions with QGIS expressions, the syntax is:

if(condition, result_when_true, result_when_false)

I would like to get an empty value (avoid creating a value at all) for the third argument, result_when_false. It should not be an empty string '' or NULL, but no value at all.

Context: When using an if-condition inside an array_foreach() function, for each element with empty output (result_when_false), a '' or NULL is added. How to avoid that and add to the array only an element if the condition is true – thus only the result_when_true outputs?

What I tried and what works, but is more of a workaround is to use array_remove_all() to delete the '' or NULL elements from the array.

However: is there a simpler option to avoid getting an output for result_when_false?

Best Answer

It seems to me that if() and CASE statements are designed to throw up NULL rather than skip a value entirely (so contrary to the comment to your question, not including an ELSE still returns a NULL for non-matching values) And I think that is the preferable outcome as it would be dangerous otherwise.

Perhaps try array_filter() with your condition test, and then if you need to transform the results further you can apply array_foreach() to the remaining values.