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?
2 Answers 2
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.
Running the model with input character a
: all features with attribute name
starting with a
are selected:
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
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.
-
1The single quotes are important, although not intuitive. But this is actually the correct answer.webrian– webrian2023年11月10日 08:41:45 +00:00Commented Nov 10, 2023 at 8:41