I am trying to compare the values of 2 fields in my attribute table and update a 3rd field as a result. In the field calculator I have

CASE  when (("Address")  like  ("COUNTYNAME" )) then 'Match'
else 'Error'

so for the following toy data examples I would get the result Match,Error,Error Address,COUNTYNAME

  • Smith, Smith County
  • Bob, Smith County
  • Jane, Bob County

I have also tried CASE when (("Address") IN ("COUNTYNAME" )) then

Are there any other ways to compare text values within QGIS?

2 Answers 2


You can do something like this to compare two fields together:

When  "Address" in (left( "CountyNM", strpos("CountyNM", ' ')-1)) Then 'Match'
Else 'Error'

The strpos() function will return the index position of the first ' ' (space) character and the left() function "trims" the string before that position. The -1 is to ensure that the space will not be included when doing comparison.

Here is the output of the above expression:

enter image description here


The correct syntax is :

CASE WHEN "Address" LIKE ('%' || COUNTYNAME|| '%') THEN 'Match' ELSE 'Error' END

for the LIKE fonction to work you need to concatenate (using '||' ) the '%' wildcard (replace 0 to n character) with your field name, also you could add some UPPER or LOWER to convert to all uppercase (or all lowercase) to avoid false negative

  • Is that not comparing the value from Address with a stationary COUNTYNAME instead of comparing the values in row [x] of field Address with field COUNTYNAME
    – Michael
    Apr 3, 2018 at 16:04
  • This should compare the value of row(x) of the Address field with the value of row(x) of the COUNTYNAME field .
    – J.R
    Apr 3, 2018 at 16:19
  • 1
    There is no ' around COUNTYNAME so it's taken as a field name, if you want you could put " around the field name it will work the same...
    – J.R
    Apr 3, 2018 at 16:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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