Select Layer by Attribute issues

2906
7
Jump to solution
10-23-2017 10:08 PM
ShelbyHines
Esri Contributor

Hi all,

I am trying to 1) Select by Attribute and 2) overwrite a feature class with selected features.  Below is the script that I'm trying as a start.. and am getting an error related to Select by Attribute.  Ultimately, need to select last 365 days of data (not Baker neighborhood) and overwrite a feature class.. on a daily basis.  Thank you in advance for any assistance!  Shelby

# Description: Extract features to a new feature class based on an attribute query
# Import system modules
import arcpy

# Make a layer from the feature class
arcpy.MakeFeatureLayer_management ("D:/pd/crimeD.gdb/dencrime", "crimelayer")

# Select all crimes that happened in Baker
arcpy.SelectLayerByAttribute_management ("crimelayer", "NEW_SELECTION", ' "NEIGHBORHOOD_ID" = baker ')
# Write the selected features to a new featureclass
arcpy.CopyFeatures_management("crimelayer", "Bakercrime")

0 Kudos
1 Solution

Accepted Solutions
XanderBakker
Esri Esteemed Contributor

It is likely the query you are using. the value Baker should be between quotes and the field name (if FC is stored in fgdb) would be without quotes. Something like this:

arcpy.SelectLayerByAttribute_management ("crimelayer", "NEW_SELECTION", "NEIGHBORHOOD_ID = 'baker'")

Sorry no access to my computer at this time to verify a little more.

View solution in original post

7 Replies
DanPatterson_Retired
MVP Esteemed Contributor

With the shell script what is the error just in case

  • there is an error in the script and more importantly
  • it wouldn't work in the first place because what you are querying doesn't exist

Secondly...

  • how is the time data stored? 
  • is it in a field?
  • sequential values?
  • is it a date field or a string field?

More questions pending the answers to the above.

ShelbyHines
Esri Contributor

Thank you Dan!

This is the error that I'm getting:

Traceback (most recent call last):
File "<string>", line 7, in <module>
File "c:\program files\arcgis\pro\Resources\arcpy\arcpy\management.py", line 6569, in SelectLayerByAttribute
raise e
File "c:\program files\arcgis\pro\Resources\arcpy\arcpy\management.py", line 6566, in SelectLayerByAttribute
retval = convertArcObjectToPythonObject(gp.SelectLayerByAttribute_management(*gp_fixargs((in_layer_or_view, selection_type, where_clause, invert_where_clause), True)))
File "c:\program files\arcgis\pro\Resources\arcpy\arcpy\geoprocessing\_base.py", line 506, in <lambda>
return lambda *args: val(*gp_fixargs(args, True))
arcgisscripting.ExecuteError: ERROR 000358: Invalid expression
Failed to execute (SelectLayerByAttribute).

Regarding your other questions, the date is in a field and is "date" data type.  Yes, they are sequential dates from 2010 through today.  Hundreds per day.

Much appreciated, Shelby

0 Kudos
XanderBakker
Esri Esteemed Contributor

It is likely the query you are using. the value Baker should be between quotes and the field name (if FC is stored in fgdb) would be without quotes. Something like this:

arcpy.SelectLayerByAttribute_management ("crimelayer", "NEW_SELECTION", "NEIGHBORHOOD_ID = 'baker'")

Sorry no access to my computer at this time to verify a little more.

ShelbyHines
Esri Contributor

Thank you Xander, this worked!  

0 Kudos
ShelbyHines
Esri Contributor

Hi Dan_Patterson, I wonder if you know how to query for data from the last 365 days using a date field?  Thank you!  -Shelby

0 Kudos
DanPatterson_Retired
MVP Esteemed Contributor
import datetime

n = datetime.datetime.now()  # ---- now 

t = datetime.timedelta(days=365)  # ---- used timedelta options 

print("Now  {} \nThen {}".format(n, n-t))

Now  2017-10-24 15:05:40.710201 
Then 2016-10-24 15:05:40.710201

# ---- see the help for working with arcpy time objects

Shelby.... To give you some ideas

ShelbyHines
Esri Contributor

Thank you for this, will be testing this!  Was able to make it work with the help of a colleague: 

arcpy.SelectLayerByAttribute_management("crimelayer", "NEW_SELECTION", "REPORTED_DATE > timestamp '{0}'".format(str(year_ago)), None)

0 Kudos