4

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?

2
  • 2
    You could try: CASE WHEN conditon THEN result END
    – pathmapper
    Commented Nov 7, 2021 at 17:28
  • 1
    Might have to put it into a slightly larger custom function? test for the condition, set true and false results to variable x and z... (true and false) , but then enter second step to populate array with value set inside of x.... or something similar.
    – nr_aus
    Commented Nov 9, 2021 at 8:43

1 Answer 1

4

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.