Knowledge Base - Technical Articles


Technical Article   HowTo:  Keep leading zeros for zip codes in ArcGIS attribute tables

Article ID: 36082
Software:  ArcGIS - ArcEditor 8.1, 8.1.2, 8.2, 8.3, 9.0, 9.1, 9.2, 9.3, 9.3.1 ArcGIS - ArcInfo 8.0.1, 8.0.2, 8.1, 8.1.2, 8.2, 8.3, 9.0, 9.1, 9.2 ArcGIS - ArcView 8.1, 8.1.2, 8.2, 8.3, 9.0, 9.1, 9.2, 9.3, 9.3.1
Platforms:  Windows XP, Vista

Summary

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.

Procedure

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.

  1. In ArcMap, right-click the feature class or table in the table of contents and click 'Open' or 'Open Attribute Table'.
  2. At the bottom of the Attribute Table dialog box, click 'Options' then select 'Add Field'.
  3. 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.
  4. Scroll the Attribute Table to the right, and right-click the new field and select Field Calculator. Click Yes to the warning.
  5. 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.
  6. Click OK to run the field calculator. The field should now be populated.
  7. Add another new field called 'length' and set the data type to 'double'.
  8. Open the field calculator of the 'length' field.
  9. Enter: len([Zip_Code_Text_Field]) and click Ok. This gives the string length of the zip code field.
     [Zip_Code_Text_Field] refers to the name of the new text field containing the zip code information in brackets.

  10. In the Attribute table, click Options, then click Select by Attribute. Select all of the features with a string length of 4 using the syntax below:

    [Length]=4

  11. Open the field calculator of the text field containing the zip codes.
  12. 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.

    “0” + [Zip_Code_Text_Field]

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

Related Information


Created: 1/9/2009
Last Modified: 5/3/2011

If you would like to post a comment, please login