HOW TO

Compare row values in two fields

Last Published: January 29, 2021

Summary

Note:
The procedure below is for ArcGIS 10.0 or later versions. Python scripts are not a supported expression type in earlier versions.

Instructions provided demonstrate how to use the Field Calculator to compare two fields on the same row of data. Fields that are different will be flagged with 0. Fields that are the same will be flagged with a 1.

Procedure

  1. Create a new field. Set the type as short or long integer and accept the other defaults.
    1. If editing, stop the current edit session.
    2. Open the feature class attribute table.
    3. Click the Table Options button.
    4. Select Add Field to open the Add Field dialog box.
    5. Name the new field.
    6. Select Long Integer from the Type drop-down list.
    7. Accept the default values for all Field Properties.
    8. Click OK.
  2. Right-click the newly created field in the step above and click Field Calculator.
  3. Select the Python parser.
[O-Image]
  1. Ensure that the Show Codeblock option is checked.
  2. Paste the following code into the Pre-Logic Script Code box:
def isSame(x, y):
    fieldA = str(x)
    fieldB = str(y)
    if fieldA == fieldB:
        return 1
    else:
        return 0
  1. Type 'isSame(!FieldA!, !FieldB!)' in the lower expression box and replace the words FieldA and FieldB with the names of the fields you want to compare.

    The results of Steps 4 - 6 should resemble the following:
[O-Image]
  1. Click OK. Fields that are the same are designated with a value of 1 and fields are different are designated with a value of 0 in the newly added field.

Article ID:000011200

Software:
  • ArcMap 10 0

Get help from ArcGIS experts

Contact technical support

Download the Esri Support App

Go to download options

Discover more on this topic