Adding zeros or numbers in a Field

4928
26
Jump to solution
10-24-2014 01:32 PM
MichelleCouden1
Occasional Contributor III

I have two fields that I need to concatenate together but I need to add zeros so one said has 4 digits and the other side has 2 digits. Both separated with a hypen. For Example, 0024-14.

 

This is the code I have written am I close???

Left([ACONTROL], 4, "-") & Right([ASECTION], 2)

 

So, it would be field ACONTROL with 4 digits and ASECTION with 2 digits.

0 Kudos
26 Replies
DanPatterson_Retired
MVP Emeritus

Well there is something wrong with your field then, I have tried it with several records from your table in python IDE itself and in the field calculator on some of my test data...see the image...
I would suggest that you delete the field you are in...recreate it as a text field with an appropriate width (type it in to make sure you get an actual field width)...make sure you aren't in edit mode and try again...something in that process is going amiss.  If that doesn't work, select the first few visible records and try again...if it works now, then you have something wrong with one of the records (is the field nullable??)

Good luck

error.png

0 Kudos
MichelleCouden1
Occasional Contributor III

Yeah, I'm not sure what is going on. I still get an error zero length field name in format. Could it be that my AControl and ASection are double fields not string??

0 Kudos
MichelleCouden1
Occasional Contributor III

Course the other thing is the first part can only be 4 numbers so how do I get it to add only the numbers I need? Also, no periods. I think I fix that by taking out the last {} right?

0 Kudos
DanPatterson_Retired
MVP Emeritus

Grief...an important detail as you can see from the python example below

>>> a = 3653.000

>>> b = 1.000

>>> "00{}-0{}".format(a,b)

'003653.0-01.0'

>>>

>>> "00{}-0{}".format(int(a),int(b))

'003653-01'

>>>

see where I added the int in front of the a and the b ... do the same around your fieldnames

0 Kudos
MichelleCouden1
Occasional Contributor III

Could this error be because it is a dbf not a shapefile or feature class??

0 Kudos
MichelleCouden1
Occasional Contributor III

Yeah, I have no idea what is going on. I just made 3 new text fields and filled the first two with the data and then tried formula on third field. It still gives me an error of "zero length field name in format".

0 Kudos
DanPatterson_Retired
MVP Emeritus

if they are text fields you don't need int, just try it on the original fields...select the first few records and use the formula with int in it.  Also, you could right-click on your two fields, go to properties and show some decimal places in the fields so you know what you really have and not what it looks like

0 Kudos
MichelleCouden1
Occasional Contributor III

OK, I'll try all of that and see how it turns out. Thank you very much for all your help today!

0 Kudos
DanPatterson_Retired
MVP Emeritus

keep at it...hope you are taking notes .

0 Kudos
MichelleCouden1
Occasional Contributor III

Oh Yeah, on the notes. I usually just print the discussion out. I'm going to continue to try to figure out this error of "zero length field name in format". Thanks again for your help.

0 Kudos