Select to view content in your preferred language

Use only date from Datetime in arcpy.SelectLayerByAttribute_management

395
2
10-09-2023 12:09 PM
Labels (3)
JeffHouser
Occasional Contributor

I'm defining a SetQuery variable in Arcpy.SelectLayerByAttribute_management(fc, 'NEW_SELECTION', SetQuery) as 

SetQuery = 'chgdate > admin_date'.  Both chgdate and admin_date are fields in the featureclass.  However, the chgdate is datetime, and admin_date is date fieldtype.  For the results from the Arcpy.SelectLayerByAttribute_management, I would like the SetQuery be based on where the chgdate values are of the date field type (not include time).   

Is there a way to do this?

-Jeff

0 Kudos
2 Replies
Kara_Shindle
Occasional Contributor III

How are you processing your data?  Can you post a Python snippet or something?  When I need a portion of a date, I parse it using the pandas package and select the portion of the date I need & store that as a new variable.  

When I do that, I use a method like this article describes

0 Kudos
JeffHouser
Occasional Contributor

Hello Kara,

Here is a code snippet that leads to the arcPy.SelectLayerAttribute_Management

fds = arcpy.ListDatasets(feature_type='Feature')
for fd in fds:
    # For each feature dataset, get a list of feature classes.
    fcs = arcpy.ListFeatureClasses(feature_dataset=fd)
    for fc in fcs:
        print ('Database: ' + fd + ' Feature Class: ' + fc)
        lstFields = arcpy.ListFields(fc)
        field_names = [f.name.upper() for f in lstFields]
        if "ADMIN_DATE" in field_names:
 
            # chgdate > admin_date.
            SetQuery = 'chgdate > admin_date'
 
            arcpy.MakeFeatureLayer_management(fc, fc + "_new")
            # print(fc+"_New")
 
            arcpy.SelectLayerByAttribute_management(fc + "_new", 'NEW_SELECTION', SetQuery)
...
 
I hope this helps to compliment my original post. 
 
-Jeff
0 Kudos