Delete the last 5 characters in a field

642
6
05-15-2014 09:53 AM
JulieHines
New Contributor II
I am trying to delete the last 5 characters in a field.
Example: MAPLE GROVE MN  55369-3466
I want to delete "-3466" where -3466 is not the value in all records.

~Julie
Tags (2)
0 Kudos
6 Replies
DanPatterson_Retired
MVP Esteemed Contributor
Python parser

>>> a = "MAPLE GROVE MN 55369-3466"
>>> b = a[:-5]
>>> b
'MAPLE GROVE MN 55369'
>>>
with appropriate changes for the field name
0 Kudos
Zeke
by
Regular Contributor III
Dan's method works if all values have a suffix like '-1234'. But if some records don't have that, e.g. they just have the 5 digit zip, you'll be getting rid of that part of the field and would end up with no zip. You might want to try:

>>> a = "MAPLE GROVE MN 55369-3466"
>>> lst = a.split('-')
>>> if len(lst) > 1:  # i.e., you have a '-' in the field
...       b = lst[0]  # this is the value you want

Even this isn't foolproof, since a '-' anywhere in the field will get caught, for instance, a street named Sans-Souci. There are ways to catch that, but trying to keep it simple.
0 Kudos
JulieHines
New Contributor II
Python parser

>>> a = "MAPLE GROVE MN 55369-3466"
>>> b = a[:-5]
>>> b
'MAPLE GROVE MN 55369'
>>>
with appropriate changes for the field name


So what if I want to delete the last 5 characters of every record in a field?
0 Kudos
Zeke
by
Regular Contributor III
So what if I want to delete the last 5 characters of every record in a field?


With the caveat I posted above:

1. In field calculator, switch the parser to Python.
2. Double click your field in the list to enter it in the calculation box.
3. Click =
4. Doubleclick your field again.
5. Add this to your field in the calculation box, no spaces between the fieldname and the following: [:-5]
6. Click Ok.

Maybe try this on a copy of your data first, just in case.
0 Kudos
JulieHines
New Contributor II
With the caveat I posted above:

1. In field calculator, switch the parser to Python.
2. Double click your field in the list to enter it in the calculation box.
3. Click =
4. Doubleclick your field again.
5. Add this to your field in the calculation box, no spaces between the fieldname and the following: [:-5]
6. Click Ok.

Maybe try this on a copy of your data first, just in case.


Does this look correct?

!OWN_ADD_L2! = !OWN_ADD_L2![:-5]
0 Kudos
Zeke
by
Regular Contributor III
Looks ok to me...but try it on a copy first, I don't know your data like you do.
0 Kudos