Search code examples
pythonselectattributessql-likearcgis

ArcGIS / Python "select by attribute" LIKE


ArcGIS 10.1, Python 2.7.2. I have a table of a few variables but 1.7 million cases. I want to select all those cases where (in ArcGIS-interface-speak)

"PCD" LIKE 'BT%'

So far no luck.

I have been trying variants on this:

whereClause = ""PCD" LIKE 'BT%'"
arcpy.SelectLayerByAttribute_management("sourceSHP", "NEW_SELECTION", whereClause)

After a lot of trial and error, this works:

arcpy.SelectLayerByAttribute_management("AllPOSTCODES","NEW_SELECTION",""""PCD" LIKE 'BT%' """)

Notes:

" single (double) quotes around the source table "
" single (double) quotes around the selection "
""" triple (double) quotes around the whereclause """
" single (double) quotes around the variable "
' single (single) quotes around the seed string '


Solution

  • This would have worked (with no triple double-quoting) too ...

    arcpy.SelectLayerByAttribute_management("AllPOSTCODES","NEW_SELECTION",' "PCD" LIKE ' + " 'BT%' ")