I am using QGIS 3.10 for Mac and facing some problems with the "Select by Expression" tool. The function I'm using is not selecting any features, even though it should. I have tested it with a few other functions, and the tool works just fine––this particular function seems to be hairy, and I'm not sure why.

I need to select features in the layer that have a certain population density. As such, I am using the following function in "Search by expression":

"TotPop_" / "sq_mi" >= 1000

I'm not sure what's wrong with the function, because it's not any selecting features at all, even though I manually went through and found a couple that apply! (The table is, unfortunately, too long for me to sift through them all manually.)

Does anyone have ideas about what I might be doing wrong?


Because you are asking to select an evaluation of the division expression to get the population density, you need to to add eval expression before apply the density:

I am expecting the following expression to work:

eval("TotPop_" / "sq_mi" >= 1000)
  • 1
    Not OP, but wondering why division requires eval() and not addition/subtraction/multiplication?
    – she_weeds
    Mar 29 '20 at 23:38
  • 1
    @she_weeds I don't know. But yes it is strange, addition/subtraction/multiplication work with / without eval() except for division.
    – ahmadhanb
    Mar 29 '20 at 23:45
  • 3
    Investigating further, I only seem to encounter this issue with dividing fields where one or both fields are integer datatype. If both are decimal/real there it works. It also works if I use to_real() around the division expression. Also, I get erroneous results when using < rather than > with integer fields - it selects all features. I think this might be a bug. I'll lodge an issue...
    – she_weeds
    Mar 29 '20 at 23:59
  • 1
    @she_weeds thanks for reporting. I agree. It looks like a bug in the expression as the issue is only limited to division expression. For the OP question, at least eval() solve the problem.
    – ahmadhanb
    Mar 30 '20 at 0:04
  • 3
    Added issue on github here. github.com/qgis/QGIS/issues/35449 Curiously it only seems to be an issue with shapefiles. I did not have the same problem with a number of other data formats.
    – she_weeds
    Mar 30 '20 at 0:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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