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
Solved! Go to Solution.
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.
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!
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]
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.
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.