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.

asked Aug 23, 2015 at 22:39
0

1 Answer 1

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)
answered Aug 23, 2015 at 23:43
2
  • 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? Commented Aug 24, 2015 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. Commented Aug 24, 2015 at 21:59

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.