Good afternoon,
I am looking for a way to enter in the where condition in an arcpy.da.searchcursor a date.
How can I search for all the records that where created in the last 5 days.
Thanks
Solved! Go to Solution.
Something like this code will help you find the date you want to limit your search from, then from there you can use the value returned from that function in a query within your search cursor 'where clause' to filter the data. The second bit of code shows one way you could do that. You'll want to read up on the datetime module and
def calc_date(daysbefore):
day = datetime.date.today() #gets todays date
longago = datetime.timedelta(days = daysbefore) #creates a datetime object for calculations
datebefore = day-longago #subtracts the number of days desired from today
return datebefore.strftime("%m/%d/%Y")#returns a text string in the form of dd/mm/yyyy)
this code is going to be buggy because this is just an example of how i think this would come together for you.
#indata is the datasource you are searching
#fieldlist is the list of fields you want returned with your query, ['*'] will return all but if there are alot you should be more specific
start = calc_date(5) #uses function above to create a start date for your query
qry = f"datefield >= '{start}'" #the sql query you'll use in the SearchCursor
with arcpy.da.SearchCursor(indata,fieldlist,sql_clause = qry)as cursor
<code you run after>
Something like this code will help you find the date you want to limit your search from, then from there you can use the value returned from that function in a query within your search cursor 'where clause' to filter the data. The second bit of code shows one way you could do that. You'll want to read up on the datetime module and
def calc_date(daysbefore):
day = datetime.date.today() #gets todays date
longago = datetime.timedelta(days = daysbefore) #creates a datetime object for calculations
datebefore = day-longago #subtracts the number of days desired from today
return datebefore.strftime("%m/%d/%Y")#returns a text string in the form of dd/mm/yyyy)
this code is going to be buggy because this is just an example of how i think this would come together for you.
#indata is the datasource you are searching
#fieldlist is the list of fields you want returned with your query, ['*'] will return all but if there are alot you should be more specific
start = calc_date(5) #uses function above to create a start date for your query
qry = f"datefield >= '{start}'" #the sql query you'll use in the SearchCursor
with arcpy.da.SearchCursor(indata,fieldlist,sql_clause = qry)as cursor
<code you run after>
SQL date functions tend to vary across data source, so there will be no single SQL that will be widely portable. See SQL reference for query expressions used in ArcGIS—ArcGIS Pro | Documentation for more information and examples. A purely Python cursor-based will be much more portable.
@JoshuaBixby thanks for interjecting that.. oracle and MS sql server can have very different looking queries, so the details could be pretty different.
this is working for me with SQL server data.
qry = """DateField >= (GetDate() - 5)"""
with arcpy.da.SearchCursor(updateFC, ['DateField'],qry) as cursor:
Do stuff on data from last 5 days.
R_
Good morning,
Thank you everyone for your answers.
I am going to query a FGDB.
from datetime import datetime, timedelta date_past = datetime.now() - timedelta(days=180) wc = "DateField < DATE '{}'".format(date_past.strftime("%Y-%m-%d %H:%M:%S")) print wc
Solved: Use date timestamp field to select features - Esri Community
For FGDB, this is also working for me:
qry = """DateField >= (current_date- 5)"""
with arcpy.da.SearchCursor(updateFC, ['DateField'],qry) as cursor:
Do stuff on data from last 5 days.
R_