Select to view content in your preferred language

Selecting Features by Date

595
5
11-13-2017 11:50 AM
MikeEdwards
Frequent Contributor

I'm trying to select all the features in a feature class that have been edited in the past day. I'm using SelectLayerByAttribute_management and I keep getting an error on the SQL query and was hoping someone could give me a hand.

In the code below lyr is the feature class being evaluated and MOD_DATE is the date field being checked.

yesterday = date.today() - timedelta(1)
yesterdaysDate = yesterday.strftime("%m/%d/%y %I:%M:%S")

SQL_statement = "MOD_DATE >= 'yesterdaysDate'"
arcpy.MakeFeatureLayer_management(lyr, "FEATURE_SELECTION")
arcpy.SelectLayerByAttribute_management("FEATURE_SELECTION", "NEW_SELECTION", SQL_statement)
0 Kudos
5 Replies
RandyBurton
MVP Alum

Try:

SQL_statement = "MOD_DATE >= '"+yesterdaysDate+"'"‍‍

And, noting Joshua Bixby‌'s comment below,  you may need to proceed your date with the word "date" (if using a file geodatabase, for example):

SQL_statement = "MOD_DATE >= date '"+yesterdaysDate+"'"
MikeEdwards
Frequent Contributor

Thank you very much. The second statement worked.

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

Seeing I don't know what back-end database is hosting your data, I can't offer a specific suggestion.  In general, dates and times are handled differently depending on the back-end database:  SQL reference for query expressions used in ArcGIS—Help | ArcGIS Desktop 

0 Kudos
TinaMorgan1
Frequent Contributor

What is the underlying database, and what is the error?

0 Kudos
MikeEdwards
Frequent Contributor

The feature classes are being pulled in through an SDE connection to an Oracel database.

0 Kudos