1

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?

ahmadhanb
41.8k5 gold badges55 silver badges110 bronze badges
asked Apr 3, 2018 at 15:15

2 Answers 2

1

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

answered Apr 3, 2018 at 16:43
0

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

answered Apr 3, 2018 at 15:59
3
  • 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 Commented 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 . Commented 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... Commented Apr 3, 2018 at 16:21

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.