Field Calculator in QGIS – Comparing String Values

attribute-tablefield-calculatorqgisstring

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'
End  

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?

Best Answer

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

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

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