1

I'm trying to find a function in the Field Calculator (preferably in VB Script) that can add numbers into a field that already has data.

I have year data ranging from the 1960s to 2014 in string format, and the only data I currently have in my field is the last two digits for each year (i.e. 60, 71, 75, 00, etc. which represents 1960, 1971, 1975, 2000).

Is there a way I can add the 19 and 20 to the correct years using the Field Calculator for the entire field?

I apologize if this is a repeat question, but I'm new to the site and couldn't find any answers.

4

I'm not sure how to do this is VB Script, but I do in python. In the field calculator, set your Parser to Python. Click the Show Codeblock box. In the input box (where you see fieldname = above), type in getYear (!fieldname!), where fieldname is the name of the field with the years.

The code block code depends on if your field is a string or a number type (int, float, etc). If it's number, this will work for you:

def getYear (val):
    if val == None:
        return
    if val > 15:
        return val + 1900
    else:
        return val + 2000

If your field is a string/text type, go with this:

def getYear (val):
   if val == None:
       return
   year = int (val)
   if year > 15:
       return str(year + 1900)
   else:
       return str(year + 2000)
  • Thanks for the advice, and I was able to get it to work! Interestingly, for some reason, the code block code would only work after I wrote the script in GUI first, then copied and pasted it into the Field Calculator. Any ideas as to why that would be? – Tula Aug 24 '15 at 21:04
  • Cool, I'm glad it works for you. The failure could be for a number of reasons. Check Geoprocessing tab -> results. In the results window, find your failed attempt and check the messages. That might give you a little hint. Oftentimes it's an indent issue. – Emil Brundage Aug 24 '15 at 21:59

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.