I have an ArcGIS table in a file geodatabase that contains a date field. I would like to write a python (arcpy) function that creates and returns a table view for a date-range given a starting and/or ending date.
Here's what I have so far:
def get_date_range_table_view(grid_table, start_date=datetime.datetime(1, 1, 1), end_date=datetime.datetime(9999, 12, 31)): min_date = '"DateTime"' + " >= date '" + str(start_date) + "'" max_date = '"DateTime"' + " <= date '" + str(end_date) + "'" expression = " AND ".join([min_date, max_date]) return arcpy.MakeTableView_management(grid_table, "date_range_table_view", expression)
The expression that uses 'start_date' works great and returns a table view that can be used with a SearchCursor. The trouble that I'm having is getting the 'end_date' logic to return a similar table view. Although the logic is identical, except for the '>=' symbol, the table view that is returned has no readable rows.
I found a similar question here: Arcpy cursors, WHERE clauses, and date/time fields
Which directs the readers to this resource (which has information about querying Date fields in ArcGIS 10.2 based on database type): SQL reference for query expressions used in ArcGIS