2

I am trying to select all values with Null by using the SelectLayerByAttribute_management. But the Fieldname in the where-clause is alterable by using the input-parameter.

That is what i tried:

if inputValue == "Null":
        ap.SelectLayerByAttribute_management(inputFeature,"NEW_SELECTION", inputFieldname +"IS NULL")

Does somebody have an idea what I did wrong? That's the error message i get: ExecuteError: ERROR 000358: Invalid expression Failed to execute (SelectLayerByAttribute).

3

Use three quotes, AddFieldDelimiters and format:

sql = """{0} IS NULL""".format(arcpy.AddFieldDelimiters(datasource=inputFeature, field=inputFieldname))
ap.SelectLayerByAttribute_management(inputFeature,"NEW_SELECTION", sql)
  • @Andreas Nice! Please accept my answer with the checkbox – BERA Jan 16 at 11:07
1

I think this should work:

sql = "{0} IS NULL".format(inputFieldname)
arcpy.SelectLayerByAttribute_management(inputFeature,"NEW_SELECTION", sql)
  • I dont think it will with shape or fgdb input since they will need double quotes around the field name. Unless inputFieldname = '"somefield"' – BERA Jan 16 at 11:20
  • 3
    @BERA My understanding is that ArcGIS has been very forgiving on field delimiters for the last 3-4 dot releases. I cannot remember the last time that I used them in an SQL expression like this with either a file geodatabase or a shapefile. – PolyGeo Jan 16 at 11:27
  • 1
    I Think you are correct. I just tried it and it does work without field delimiters. – BERA Jan 16 at 11:32

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.