Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

I want to create a new field with years data. There is an existing field with the timestamp YYYYMMDDHHMM. But I just want the years from that field to the new field. That way I can display just the years when I create a graph. IS there anyone that can explain the code to do this. I am not a programmer and need the scripts or python code explained as if I were a 5-year old. Thank you.

share|improve this question
    
What is the field type of the existing field? String? Datetime? –  AHigh Jul 11 '12 at 19:10
add comment

2 Answers

up vote 4 down vote accepted

Assuming your existing timestamp field is a string:

  1. Add a new field (Open the attribute table of your feature class>Click Upper Left Dropdown>Add Field) as a long integer.

  2. Right click new field and select "Calculate Field".

  3. Change your parser to Python and use the following expression:

    !ExistingTimeStampField![:4]

Note that you need to replace the "ExistingTimeStampField" between the exclamations with the actual name of your field (should be able to auto-populate in the Fields list box). This will trim off the leading 4 characters from your existing field which I presume will always be the 4-digit year.

Edit: If you wish to break out more information you can simply update the indices of your query like such (Note that my original [:4] is the same as saying [0:4]):

!ExistingTimeStampField![4:6]

This would return the MM in YYYYMMDDHHMMSS. [6:8] would return the DD, and so on. See here for a more detailed explanation of substrings: http://docs.python.org/tutorial/introduction.html#strings

share|improve this answer
    
Perfect, Thank you. Do you know how to parse the rest of the time (Months, days, hours, minutes) into separate fields? –  Kimball Jul 11 '12 at 20:12
1  
Do everything up until step 3 and then change the syntax to this: !ExistingTimeStampField![4:6] for months, !ExistingTimeStampField![6:8] for days, and so on. –  AHigh Jul 11 '12 at 20:17
    
Yes! Thanks a lot AHigh. –  Kimball Jul 11 '12 at 20:33
add comment

If your field type is a string that contains data formatted exactly as you said, then filling your new field is a relatively simple matter of parsing out the string.

Since you did not specify, I'll give options in both the ArcGIS, and Quantum GIS software packages, depending on what you are using.

ArcMap

In this case, the Left function would be appropriate, in which you specify the source text, and then the number of characters that you want to use from the left of the source.

Right click on the name of your new field, and choose "Field Calculator". Type in what you see from the screenshot below. Replace [CONST_DATE] with your column name. This is a VB Script option. It looks like a Python style answer has also been posted.

enter image description here

Quantum GIS

  1. Toggle Editing Mode to On - Ctrl E
  2. Open Attribute Table
  3. Open Field Calculator Field Calculator button
  4. In this case, the appropriate function is substr. In this function, you choose the source text, then specify the starting point, and the length that you want to capture. This uses an index where the first position in the string is 1.
  5. Enter text into Field Calculator box as in the screenshot below. Again, replace the field name CONST_DATE, with your appropriate source column. Also, on the right, choose the appropriate column that you are filling the date into. Field Calculator Dialog
  6. Save Edits - Ctrl S
  7. Toggle Editing Mode Off - Ctrl E
share|improve this answer
    
Perfect, Thank you. Do you know how to parse the rest of the time (Months, days, hours, minutes) into separate fields? –  Kimball Jul 11 '12 at 20:12
    
And yes I'm using ArcMap, sorry for the confusion. I was intimidated by that long answer from QGIS. –  Kimball Jul 11 '12 at 20:13
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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