Query feature if an attribute is null after 180 days of date on another attribute?

717
10
Jump to solution
10-05-2018 09:50 AM
by Anonymous User
Not applicable

Looking for a sample ArcPy script I can modify for my use. Any suggestions?

0 Kudos
1 Solution

Accepted Solutions
RandyBurton
MVP Alum

Typo on my part, an extra minus in days=180  (updated original code):

date_past = datetime.now() - timedelta(days=180)

View solution in original post

10 Replies
RandyBurton
MVP Alum

Perhaps  (you could remove time from formatting, if desired):

from datetime import datetime, timedelta

date_past = datetime.now() - timedelta(days=180) # you can add/subtract as required

wc = "Field IS NULL AND DateField < DATE '{}'".format(date_past.strftime("%Y-%m-%d %H:%M:%S"))

print wc
# Field IS NULL AND DateField < DATE '2018-04-08 09:25:35'‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍
0 Kudos
by Anonymous User
Not applicable

The month and day are correct but the year is 2019 instead of 2018?

0 Kudos
RandyBurton
MVP Alum

Typo on my part, an extra minus in days=180  (updated original code):

date_past = datetime.now() - timedelta(days=180)
by Anonymous User
Not applicable

Awesome! Thanks for your help on this.

0 Kudos
by Anonymous User
Not applicable

I used the code you supplied but noticed it's returning everything 180 days or older. I only want results that fall exactly 180 days from current date.

arcpy.SelectLayerByAttribute_management(permits, "NEW_SELECTION", "Date_Accepted = date '{}' AND Fee_Paid Is NULL".format(notice.strftime("%Y/%m/%d"))) 
0 Kudos
JoeBorgione
MVP Emeritus

My bet is to change the   <  to   =   and you should be good to go:‍



wc = "Field IS NULL AND DateField = DATE '{}'".format(date_past.strftime("%Y-%m-%d %H:%M:%S"))
‍‍‍
That should just about do it....
0 Kudos
RandyBurton
MVP Alum

When dealing with date/time, I probably wouldn't use "=" in the query.  See Joshua Bixby‌'s second comment in this thread with ESRI's explanation.

0 Kudos
JoeBorgione
MVP Emeritus

After I read you post with 'between' I realized that.....  thanks!

That should just about do it....
0 Kudos
RandyBurton
MVP Alum

And after I re-read Esri's comment, I started thinking milliseconds.  So "23:59:59" could also miss "23:59:59.9". 

0 Kudos