How To: Keep leading zeros for zip codes in ArcGIS attribute tables
Note: The content in this article pertains to ArcGIS versions 8.x and 9.x only. Later versions of ArcGIS may contain different functionality, as well as different names and locations for menus, commands and geoprocessing tools.
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.
ArcGIS attribute tables store values in several different data types, generally they are numeric, text or date.
Numeric fields can contain only numbers and can be used in calculations. Text fields can contain any combination of numbers and letters, but cannot participate in numeric calculations.
Zip codes produce a unique problem, in the sense that they are all numbers, can begin with zeros, and will not participate in calculations.
Storing zip codes in a text field eliminates this problem. However, if the data is received with the zip codes in a numeric field, then this data will need to be migrated to a text field to correct it. The field itself cannot be changed, as there is no way to change the data type of a field in ArcGIS.
Instructions provided describe how to migrate the truncated numeric data to a text field to restore the leading zeros.
- In ArcMap, right-click the feature class or table in the table of contents and click Open or Open Attribute Table.
- At the bottom of the Attribute Table dialog box, click Options then select Add Field.
- In the Add Field dialog box, name the new zip code field with a unique title. Set the Type to Text. Set the length to 10. Click OK.
- Scroll the Attribute Table to the right, and right-click the new field and select Field Calculator. Click Yes to the warning.
- Double-click the old zip code numeric field from the list of fields on the left. This places the old field name in brackets in the text box at the bottom.
- Click OK to run the Field Calculator. The field should now be populated.
- Add another new field called 'length' and set the data type to 'double'.
- Open the Field Calculator of the 'length' field.
- Enter: len([Zip_Code_Text_Field]) and click OK. This gives the string length of the zip code field.
Note: [Zip_Code_Text_Field] refers to the name of the new text field containing the zip code information in brackets.
- In the Attribute table, click Options, and click Select by Attribute. Select all of the features with a string length of 4 using the syntax below:
Code: Length = 4
- Open the Field Calculator of the text field containing the zip codes.
- Use the syntax below to add a 0 to the front of all of the fields that are only missing one “0”. Calculate only selected features.
VB Script: “0” + [Zip_Code_Text_Field] Python: “0” + !Zip_Code_Text_Field!
- Repeat steps 10 through 12, selecting the different string lengths of 3, 2 and 1, and increasing the number of zeros each time to equal a total of 5 characters. For example, if 3 is selected from the length field, then add "00" instead of "0" when calculating in the Zip Code text field.