I have an ID field that is a text datatype. The IDs in the field have a prefix, and are padded with zeros to always be a specific length. Examples:
I would like to use the Field Calculator to generate sequential IDs for new records (records are batch loaded from various sources by the thousands).
I have a python script that almost does this:
prefix = "INSP0000" lastnumber=97 rec=0 def autoIncrement(): global rec pStart = 1 pInterval = 1 if (rec == 0): rec = 1 + lastnumber else: rec += pInterval return prefix+str(rec)
- Manually enter the prefix (with zeros for padding)
- Manually enter the last number in the existing IDs
- Run the script
Unfortunately, the tool isn't smart enough to dynamically adjust for the difference in the number of digits between numbers (99 to 100, etc.):
- INSP0000100 << Problem: too many zeros/digits
How can I use the field calculator to generate sequential, prefixed IDs that are padded with zeros to a specific length?
I'm not married to Python; VBScript would be an acceptable alternative.