[GIS] using field calculator on string field that has some empty values

arcgis-desktopfield-calculatorstringvbscript

I have a CityState field that has the city and states of landowners. Bellingham WA, for example. I am making two new fields, OwnerCity and OwnerState in which I strip the last two characters of CityState for OwnerCity (RTrim([CityState],2) and capture the last two characters for OwnerState (Right([CityState],2). Some of the CityState cells are empty, so I can't run the calculator.

What can I add so that it runs the calculation where there is text, and keeps the value empty if there is not any text? I'm sure there's an easy answer to this, but I have't figured it out yet.

Best Answer

Using python parser in field calculator you may use the following functions to run the city/state splits and ignore those records with empty values (I know this is not vbscript but it is very easy to implement too):

Note, it would probably be better to have "," delineating the city/state names in the CityState field to ensure a more robust split for cities that have multiple names / spaces.

RUN on new city field:

def splitValue(x):
  if ' ' in x:
    return x.split(' ')[0] # assuming you have a space between city and state, you could also split by , too
  else: return x

splitValue(!CityState!)

RUN on new state field:

def splitValue(x):
  if ' ' in x:
    return x.split(' ')[1] # assuming you have a space between city and state, you could also split by , too
  else: return x

splitValue(!CityState!)