In QGIS, most likely from the SELECT FEATURES BY VALUE...(F3) or SELECT FEATURES BY EXPRESSION...(CTRL-F3), what is the simplest most flexible way to put in multiple values and have QGIS show that selection on the canvas?

I have list of comma separated values which are the values of features I want to select from a spatial dataset. 1, 2, 3, 4, 5. Because it might be a series of 50 values, I want to quickly paste that into an expression, without adding single quotes or the like.

I'm thinking array_to_string is part of the story, but not seeing any example of how that goes together. Unnest doesn't seem to be a supported function. My test below.

Or possibly in the DBManager as SQL, however I think my colleagues will not adopt that as readily.

enter image description here

  • 3
    Are the values always numbers if so you don't need single quotes. "targetfield" IN (1,2,3,4,5) will work. – Baswein Jun 29 at 23:31
  • Hey @Baswein that's a valid answer! Many thanks. – BJW Jun 29 at 23:42
  • Didn't realise I was so close. Now if you could consider a supplementary question, what if the array comes as space separated values, not comma separated? – BJW Jun 29 at 23:43
  • 1
    I would have thought that this would work but it doesn't for some reason: "targetfield" IN(string_to_array('1 2 3 4 5',' ',',') Perhaps because it comes out as a string. Not sure. Also tried "targetfield" IN(replace('1 2 3 4 5',' ',',') – Baswein Jun 30 at 1:43
  • 1
    @Baswein please add your solution as an answer so people can up vote it and BJW can accept it if it solves his problem – Ian Turton Jun 30 at 7:34

If the values are always numbers you don't need single quotes so simply using.

"targetfield" IN (1,2,3,4,5)

In select by expression will work.

| improve this answer | |
  • I am not getting consistent results, I put in say 20 values and QGIS selects 50 - 100. Not good. – BJW Jul 7 at 8:31
  • That's strange. Are there duplicate numbers? Are they multipart geometries? You should be able to look at the attribute table and see what numbers are being selected and trouble shoot from there. – Baswein Jul 7 at 12:00
  • I've put the inconsistency down to something wrong with one install of QGIS. – BJW Jul 7 at 20:10

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.