1

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.

  • 3
    Use an if statement to make sure the length of CityState is not zero (or greater than two) before attempting to trim it. – kenbuja Aug 7 '15 at 16:24
  • What's the best way to do this using VB script? – britt86 Aug 7 '15 at 16:34
2

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!)

  • I have tried these, and continue to run into errors. This is what I used in the field calculator with the Python box checked. Unfortunately this was county parcel data which did not use commas as delimiters, but spaces instead. def splitValue( !CityState! ): if ' ' in !CityState! : return !CityState!.split(' ')[0] else: return !CityState! – britt86 Aug 10 '15 at 17:40
  • Your def function statement itself cannot have the field name and exclamation marks as function arguments. Rather you need to just include an argument (e.g. (x) ) that will source the input field values !CityState! – artwork21 Aug 10 '15 at 20:23
1

There is also a split function in VB Script.

So, your code would look something like (VB Script Parser Checked and Show Codeblock checked):

Pre-Logic Script Code:

Dim result
a = Split([YOUR FIELD], " ") 'first parameter is field name, 2nd is delimter
result = a(0) 'or a(1) depending on what part of the split string you need

FIELDNAME = Part

result

For Missing Values

If you know that you may be missing a field value, you can add in a check for that empty value. I found using ubound(a) to give you the upper bound of the array created by the Split function. So, to check if the function wasn't able to split (only 1 value, no spaces, etc), you can do something like:

if ubound(a) = 0 then
result = "whatever"
end if

You may find that the python route is much easier since you can check the .length property of the array instead of having to call this other function.

It is a little trickier than the Python parser, but you may find it easier if you're more familiar with VB Script. ESRI's examples are awfully lacking good VB Script examples, but it looks like you should be able to take the couple simple ones they have and expand on it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.