QGIS Attribute Table – Compare Strings and Extract Unique Values

arrayattribute-tableqgisstring

In QGIS 3.18. I am looking for a field calculator expression able to return the values of a string (comma separated) in Field B which are not* also values of a string in Field A. For example:

  • Field A: 2,4,6,8,10
  • Field B: 1,2,3,4,5
  • Field C (desired result of expression): 1,3,5

I suppose this will be a situation for string_to_array and then an array function, but array_distinct returns distinct values of one single array; perhaps there is a way to structure the expression such that it will compare the 2 cells, but I've not yet found it.

Best Answer

Use this expression:

array_to_string (
    array_remove_all(
        array_foreach (
            string_to_array(field_B),
            if (
                array_contains (string_to_array(field_A),@element),
                '',
                @element
            )
        ), 
        ''
    )
)

enter image description here