Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

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. Some help please. Using ArcInfo 10.1

share|improve this question

4 Answers 4

up vote 4 down vote accepted

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.

share|improve this answer

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()

share|improve this answer

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.

share|improve this answer
    
Another way to add leading zeroes is using the zfill method on a string: str(2451).zfill(5) will result in "02451". –  dmahr May 22 '13 at 14:59
    
And if you want Python code instead of VB Script, use !Existing_ZIP_CODE_FIELD!.zfill(5) if existing field is already a string, or "{:05d}".format(!Existing_ZIP_CODE_FIELD!) if it's an integer. –  nmpeterson May 22 '13 at 15:03

I think this page has the information you needed about how to keep leading zeros for zip code field.

share|improve this answer

We're looking for long answers that provide some explanation and context. Don't just give a one-line answer; explain why your answer is right, ideally with citations. Answers that don't include explanations may be removed.

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.