How To: Compare row values in two fields


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.


  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.
  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
        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:
  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.

Last Published: 9/23/2019

Article ID: 000011200

Software: ArcMap 10.4.1, 10.4, 10.3.1, 10.3, 10.2.2, 10.2.1, 10.2, 10.1, 10