7

I am trying to use select by expression in QGIS graphical modeler and use an input string as value inside the expression. So the model will ask the user for input (which value) and this should then be used in the algorithm.

The thing is, if I write the expression in the model, then the algorithm does work: "gemnaam" ILIKE 'A%' does select all features with attribute gemnaam starting with an A. If I use "gemnaam" ILIKE @firstchar no features are selected.

Now, when I use Extract by expression in stead, this algorithm does work with @firstchar

Is it a bug or default behaviour that Select by expression does not work with variable user input like @firstchar?

enter image description here

2 Answers 2

6

If you want to define an input as a variable, first create an input, than use this input with Set project variable, give it a name and use the value of the input with Using model input (see screenshot).

You can than use this variable with it's name in select by expression. Be sure that in the Select by expression dialog box (bottom left on the screenshot) under Dependencies you have selected the Define variable algorithm. Otherwise, you have to run the model twice to update the variable to the new input.

enter image description here

Running the model with input character a: all features with attribute name starting with a are selected:

enter image description here


If you have a pre-defined project-variable, this solution applies:

You forgot the '%' part. Use this expression:

"gemnaam" ILIKE @firstchar || '%'

Or use this alternative:

left ("gemnaam",1) = @firstchar
0
1

The issue is actually simpler, when using a variable and ones that depend on previous processes, switch from "Value" to "Pre-calculated Value" in the expression dropdown then wrap your expression in single quotes.

enter image description here

1
  • The single quotes are important, although not intuitive. But this is actually the correct answer.
    – webrian
    Commented Nov 10, 2023 at 8:41

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.