QGIS Modeler – Using Variable Input in Select by Expression

expressionqgisqgis-modeler

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

Best Answer

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
Related Question