Within AGOL Notebook I'm trying to query a date field in my feature layer and am having difficulty nailing down the correct syntax for the Where clause.
I'm trying to get all features that have been updated (edited) in the last week from the day the script is run.
Below is what I currently have in the Notebook cell for setting up the date variable (to be one week ago) and running the feature layer query code. The code fails on the line with the query and I'm sure it has to do with the where clause. "timestamp" was a recent addition and I definitely am not sure if it belongs in the clause.
I've verified that my lastWeek variable gets the correct date value.
now = datetime.datetime.now()
lastWeek = now - datetime.timedelta(days = 7)
query_result1 = addr_fl.query(where="LASTUPDATE >= timestamp 'lastWeek'", out_fields='LOCID,ACTIVE,ADDRESS,LASTUPDATE')
Any help with this where clause syntax would be much appreciated.
Solved! Go to Solution.
I think your outfields need to be a list and you may have to clean up your timestamp
now = datetime.datetime.now()
lastWeek = (now - datetime.timedelta(days = 7)).strftime("%Y-%m-%d %H:%M:%S")
out_fields = ['LOCID','ACTIVE','ADDRESS','LASTUPDATE']
query_string = f"LASTUPDATE >= TIMESTAMP '{lastWeek}'"
query_result1 = addr_fl.query(where=query_string, out_fields=out_fields)
I think your outfields need to be a list and you may have to clean up your timestamp
now = datetime.datetime.now()
lastWeek = (now - datetime.timedelta(days = 7)).strftime("%Y-%m-%d %H:%M:%S")
out_fields = ['LOCID','ACTIVE','ADDRESS','LASTUPDATE']
query_string = f"LASTUPDATE >= TIMESTAMP '{lastWeek}'"
query_result1 = addr_fl.query(where=query_string, out_fields=out_fields)
Thank you! The where clause in your code has my answer. I neglected to put my variable in curly braces and I did not have the "f" at the front of my clause...that made the difference. My out_fields were ok though.