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?
2 Answers 2
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:
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 COUNTYNAMEMichael– Michael2018年04月03日 16:04:44 +00:00Commented 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– J.R2018年04月03日 16:19:15 +00:00Commented Apr 3, 2018 at 16:19
-
1There 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– J.R2018年04月03日 16:21:35 +00:00Commented Apr 3, 2018 at 16:21
Explore related questions
See similar questions with these tags.