Select to view content in your preferred language

result suppressing starting zeros in calculating field...

2043
4
Jump to solution
09-17-2014 04:25 AM
SunilChandekar
Deactivated User

Dear mates,

I am facing a problem in (ArcMap 10) calculating a field in attribute table. Want to concatenate 2 fields in new field which is 5 digit.

First input field is in 2 digit and another one is in 3 digit. I could concatenate (Using "&" in field calculator) properly but the result is suppressing starting Zeros from input fields.

for ex. Input field1 = 09 and Input field2 = 009 then result field = 99, it should be 09009.

any suggestions..???

Thanks',

Sunil

0 Kudos
1 Solution

Accepted Solutions
DanPatterson_Retired
MVP Emeritus

In pure python, this is the example using the format approach

>>> a = 9; b = 9

>>> "0{0[0]}00{0[1]}".format([a,b])

'09009'

Now the whole formatting sequence is enclosed in either single or double quotes ... that is...


"....formatting parameters....".format( ...stuff to format...)

In the example, stuff to format is a list of 2 numbers, a = 9 and b = 9.

The first sequence of really strange looking numbers and squiggles and numbers ...

0{0[0]}  means pad with a 0, then take the first element from out list {0[0]}

The sequence can be interpreted the same, except we are padding with 00 and we are using the second list element...

00{0[1]}

Now you say....spread it out it is tooo hard to read...so we will do it and demonstrate the result of

>>> " 0 {0[0]}  00 {0[1]}".format([a,b])

' 0 9  00 9'

which is obviously not what we wanted. So consider two integer fields IntFld and IntFld1 going to a text field Result

"0{0[0]}00{0[1]}".format([ !IntFld! , !IntFld2! ])

in the field calculator yields a string, yields ...      09009 regardless of what the field looks like

If the field is already in the correct format then you can simply use "{0[0]}{0[1]}".format([ !txtFld! , !txtFld2! ]) to concatenate them or if they aren't as in the image you can use  "0{0[0]}00{0[1]}".format([ !txtFld! , !txtFld2! ])‌ as before.

FormatFieldCalc1.png

View solution in original post

0 Kudos
4 Replies
AnthonyGiles
Honored Contributor

Sunil,

I cannot seem to replicate your issue (but I am using 10.1), have you tried switching the parser to python and use:

!field1! + !field2!

0 Kudos
RichardFairhurst
MVP Honored Contributor

in all likelihood the leading zeros are not part of the field value if the fields are numeric and are just a display property of the layer that has no effect on the values you are concatenating.  If they are real numeric values and not string values then in order to replicate the layer display properties in the concatenated string values the formula in VB Script needs to be:

Left("00", 2 - Len([field1])) & [field1] & Left("000", 3 - Len([field2])) & [field2]

0 Kudos
TedKowal
Honored Contributor

If you are using vbscript....make sure the field that this is being calculated to is a string.

CStr([Field1]) & CStr([Field2])

  It would help is you indicate the script snippet you are using to concatenate the two fields.

0 Kudos
DanPatterson_Retired
MVP Emeritus

In pure python, this is the example using the format approach

>>> a = 9; b = 9

>>> "0{0[0]}00{0[1]}".format([a,b])

'09009'

Now the whole formatting sequence is enclosed in either single or double quotes ... that is...


"....formatting parameters....".format( ...stuff to format...)

In the example, stuff to format is a list of 2 numbers, a = 9 and b = 9.

The first sequence of really strange looking numbers and squiggles and numbers ...

0{0[0]}  means pad with a 0, then take the first element from out list {0[0]}

The sequence can be interpreted the same, except we are padding with 00 and we are using the second list element...

00{0[1]}

Now you say....spread it out it is tooo hard to read...so we will do it and demonstrate the result of

>>> " 0 {0[0]}  00 {0[1]}".format([a,b])

' 0 9  00 9'

which is obviously not what we wanted. So consider two integer fields IntFld and IntFld1 going to a text field Result

"0{0[0]}00{0[1]}".format([ !IntFld! , !IntFld2! ])

in the field calculator yields a string, yields ...      09009 regardless of what the field looks like

If the field is already in the correct format then you can simply use "{0[0]}{0[1]}".format([ !txtFld! , !txtFld2! ]) to concatenate them or if they aren't as in the image you can use  "0{0[0]}00{0[1]}".format([ !txtFld! , !txtFld2! ])‌ as before.

FormatFieldCalc1.png

0 Kudos