Select to view content in your preferred language

How to search for Today's date minus 5 with arcpy.da.searchcursor

993
6
Jump to solution
10-23-2024 11:31 AM
JoseSanchez
Frequent Contributor

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

 

 

 

0 Kudos
1 Solution

Accepted Solutions
clt_cabq
Frequent Contributor

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> 

 

View solution in original post

6 Replies
clt_cabq
Frequent Contributor

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> 

 

JoshuaBixby
MVP Esteemed Contributor

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. 

clt_cabq
Frequent Contributor

@JoshuaBixby thanks for interjecting that.. oracle and MS sql server can have very different looking queries, so the details could be pretty different. 

0 Kudos
RhettZufelt
MVP Notable Contributor

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_

 

0 Kudos
JoseSanchez
Frequent Contributor

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

 

0 Kudos
RhettZufelt
MVP Notable Contributor

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_

0 Kudos