TransWikia.com

Adding "0" to front of all values in column using ArcGIS Field Calculator

Geographic Information Systems Asked on August 3, 2021

In ArcInfo 10.1 I have a table for each state with Zip Code values. The 5-digit Zip Codes that start with "0" have been reduced to 4-digits. I cannot figure out how to add a "0" to the front of all the values in the table. Tried a number of field-calculations but just can’t seem to get it right.

6 Answers

As far as I know, if you are storing zip codes in a numerical field (integer, float, etc.) it will always strip the preceding zero. The only way I have found to deal with this is to store zip codes in a text column instead.

Correct answer by Brian on August 3, 2021

I think How To: Keep leading zeros for zip codes in ArcGIS attribute tables has the information you needed about how to keep leading zeros for zip code field.

Zip codes are exclusively numbers, and are often stored in a numeric (formatted) field. When a zip code with leading zeros is entered in a numeric field, the leading zeros are omitted, therefore creating an incorrect zip code. ArcMap tries to match the data type with the appropriate field types for imported tables, often causing zip codes to be designated to a numeric field.

Answered by Vanni Zhang on August 3, 2021

As Brian mentioned you will need to store the data in a text field. Then the field calculation is: right("00000" + [Existing_ZIP_CODE_FIELD],5)

this will ensure you have all of the necessary leading 0s up to 5 characters.

Answered by Hotpepper on August 3, 2021

As @Brian has said, the field will have to be text to keep the zero on the left. Here's a quick, basic python example:

import arcpy

#add path to shapefile, feature class
fc = "..."

arcpy.AddField_management(fc, "ZIP5", "TEXT", "", "", 5)

rows = arcpy.UpdateCursor(fc)
for row in rows:
    #row.ZIP4 should be changed to the field
    #with your ZIP, ex: row.ZIP_CODE
    new_value = str(row.ZIP4)[:5].rjust(5,"0")
    row.ZIP5_2 = new_value
    rows.updateRow(row)
del row
del rows

This makes use of UpdateCursor, Add Field (Data Management), and Python string formatting, particularly .rjust()

Answered by gm70560 on August 3, 2021

use & instead of + for this to work in VBA right("00"& [CID],2)

Answered by Jaydeep Lakkad on August 3, 2021

Using python not the field calc but still works.

Used for dynamically adding leading zeros to a field based on the desired number of leading zeros and the length of the string with basic error handling

For example:

  • 2 = 00002
  • 89 = 00089
  • 187 = 00187
  • 2098 = 02098

Note the field must be Text otherwise there is no point into running this.

Run in ArcMap

fc_name = 'NAME'
fields = ["FIELD_NAME"]

desired_length = 5

with arcpy.da.UpdateCursor(fc_name, fields) as cursor:
    for row in cursor:
        if row[0].isdigit():
            if len(row[0]) < desired_length:

                row[0] = row[0].zfill(desired_length)
                print row
                cursor.updateRow(row)

            elif len(row[0]) > desired_length:
                print ("> Lenght - Manually Check: " + row[0])
        else:
            print ("NAN - Manually Check: " + row[0])

print ("Finished")

Answered by Tristan Forward on August 3, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP