I am trying to select a layer by attribute (field is null) using a stand alone python script.

    #Select layer by attribute to find any null values
    arcpy.AddFieldDelimiters(mayIntLyr, "DEVICE_ID")
    select = '"DEVICE_ID" IS NULL'
    arcpy.SelectLayerByAttribute_management(mayIntLyr, "NEW_SELECTION", select)

It keeps returning error 000358, indicating the expression is invalid. Not sure what is wrong with my expression, and I tried adding field delimiters as well and it still doesn't work.

I am using ArcGIS 10.4, and have attempted to incorporate an answer from a similar thread to my code (shown below) to allow for null values, blank values, and spaces, but it still doesn't work.

     select = "DEVICE_ID IS NULL and DEVICE_ID != '' and DEVICE_ID != ' '"
  • What version of ArcGIS are you using? – smiller Aug 20 '18 at 16:20
  • This thread may be helpful: gis.stackexchange.com/questions/233691 – smiller Aug 20 '18 at 16:21
  • Please Edit the question in response to requests for clarification. It's not fair to those who would answer to need to mine the comments for critical information. – Vince Aug 20 '18 at 17:13

In the first example, you do not need the quotations around the field name. As such, your statement should look like this:

    select = 'DEVICE_ID IS NULL'

In the second example, if I recall correctly, the ArcGIS 10.x inline SQL syntax doesn't use != (that's a Python thing in Arc), but rather uses <> for "does not equal". As such, your second statement should be as follows:

    select = "DEVICE_ID IS NULL and DEVICE_ID <> '' and DEVICE_ID <> ' '"

I hope this helps!


I did some looking at how AddFieldDelimiters works, and I don't think you're using it as intended. It returns a string containing proper field delimiters for whatever format your data is in (File GDB/Shapefile, Personal GDB, Enterprise GDB). Try this for the first example you gave:

    fieldName = arcpy.AddFieldDelimiters(mayIntLyr, "DEVICE_ID") #Assigns your delimited field name to a variable
    select = """{0} IS NULL""".format(fieldName) #Formats that delimited name into the query
    arcpy.SelectLayerByAttribute_management(mayIntLyr, "NEW_SELECTION", select)

Also, I looked more closely at your second piece of code you posted. Are you trying to get any that are Null, along with any that are blank or single spaces? If so, it should look like this:

    select = """{0} IS NULL OR {0} = '' OR {0} = ' '""".format(fieldName)

Note that the triple quotes around each SQL Query allow for use of single or double quotes inside them (if your field delimiters need them). I sure hope this works for you... it should!

| improve this answer | |
  • I tried both of your suggestions, but it's still coming up with the same error. Thanks though. – T.Law Aug 20 '18 at 22:11
  • Please check out the update I added to my answer! From what I can tell, it should do what you're trying to do... – flintlockspecial Aug 21 '18 at 17:24


import arcpy
fc = r'C:\folder\database.gdb\feature_class'
mayIntLyr = 'somename'

arcpy.MakeFeatureLayer_management(in_features=fc, out_layer=mayIntLyr) #To select you need a layer, not a feature class
sql = """{0} = ' ' OR {0} IS NULL""".format(arcpy.AddFieldDelimiters(mayIntLyr, "DEVICE_ID"))
arcpy.SelectLayerByAttribute_management(mayIntLyr,where_clause = sql)
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.