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

155
10
Jump to solution
10-05-2018 09:50 AM
vonmoosa
MVP

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

0 Kudos
1 Solution

Accepted Solutions
RandyBurton
MVP Regular Contributor

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 Regular Contributor

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
vonmoosa
MVP

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

0 Kudos
RandyBurton
MVP Regular Contributor

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

vonmoosa
MVP

Awesome! Thanks for your help on this.

0 Kudos
vonmoosa
MVP

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 Esteemed Contributor

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"))
‍‍‍
can't wait to retire....
0 Kudos
RandyBurton
MVP Regular Contributor

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 Esteemed Contributor

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

can't wait to retire....
0 Kudos
RandyBurton
MVP Regular Contributor

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