Selecting records from table's date field and deletiing expired dates.

480
1
09-18-2013 01:13 PM
ChuckShaw
New Contributor II
I have zero programming skills here, so I need some help.  I have a SDE Feature Class (SQL) showing where current Public Works Department's Projects are currently taking place though out the city.  When a project location is added, someone populates the "begdate" field (Project Start Date) and the "enddate" field (Project Completion Date) for that project record.  I want to write a script/program that will query the "enddate" field looking for records where the dates have expired. If it's older than today's date on my computer, then those records are selected and deleted from this Feature Class.  Where do I start?  Any suggestions would be appreciated.

Thanks,
Chuck
0 Kudos
1 Reply
by Anonymous User
Not applicable
Hi Chuck,

This can be done very easily with Python.  There are a few modules that give you access to time objects such as the a" rel="nofollow" target="_blank">http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#//002... scheduled task.  But first a few questions:

1.  What version of ArcGIS are you using?
2. What is the format of your date field (i.e. string, date)?

Here is a code sample for using version 10.1 or 10.2 for if you have your date field in a String/Text format:

import arcpy, os, time

arcpy.env.overwriteOutput = True

# today's date
today = time.strftime('%Y/%m/%d')
print "Today's Date: %s" %today

# table
table = r'C:\TEMP\Stream_points.shp'

# Update Cursor to delete outdated rows ('Str_date' is name of date field in text format)
count = 0
with arcpy.da.UpdateCursor(table, ['Str_date']) as rows:
    for row in rows:
        if row[0] < today:
            rows.deleteRow()
            count += 1

# Print results of how many rows were deleted
print 'Removed %s records for expired projects' %count
    



And here is how you would do it if it is a date type field:

import arcpy, os, datetime

arcpy.env.overwriteOutput = True

# today's date
today = datetime.datetime.today()
print "Today's Date: %s" %today

# table
table = r'C:\TEMP\Stream_points.shp'

# Update Cursor to delete outdated rows ('Prjr_date' is name of date field in date format)
count = 0
with arcpy.da.UpdateCursor(table, ['Prj_date']) as rows:
    for row in rows:
        if row[0] < today:
            rows.deleteRow()
            count += 1

# Print results of how many rows were deleted
print 'Removed %s records for expired projects' %count
    


Good luck!
0 Kudos