ArcPy SelectLayerByAttribute_management with a domain

328
6
Jump to solution
04-30-2020 12:03 PM
BillChappell
Occasional Contributor II

Hi,

I want to subset my database and when I tried 

arcpy.SelectLayerByAttribute_management ("OHlyr3", "NEW_SELECTION", " [SUBTYPECD]" LIKE \'%Overhead\' ) 

I was getting an error on the sql expression. Than I found out the field "SUBTYPECD" is really a domain. 

What I need is to query against part of it's value like Overhead where I have "Single Phase Overhead" as a domain value  It's coded number is 2 if that's easy. 

Any ideas?

 

Tags (2)
0 Kudos
1 Solution

Accepted Solutions
JoeBorgione
MVP Esteemed Contributor

I found out the field "SUBTYPECD" is really a domain. 

The field isn't a domain, it's a field that is controlled by a domain.  What happens if you write your query like this:

select = 'SUBTYPECD = 2'

arcpy.SelectLayerByAttribute_management ("OHlyr3", "NEW_SELECTION", select) 
That should just about do it....

View solution in original post

6 Replies
JoeBorgione
MVP Esteemed Contributor

I found out the field "SUBTYPECD" is really a domain. 

The field isn't a domain, it's a field that is controlled by a domain.  What happens if you write your query like this:

select = 'SUBTYPECD = 2'

arcpy.SelectLayerByAttribute_management ("OHlyr3", "NEW_SELECTION", select) 
That should just about do it....
DavidPike
MVP Frequent Contributor

I started typing before you replied Joe, honest!

0 Kudos
BillChappell
Occasional Contributor II

This works, it's the simple solution that I overlooked. Instead I tried 20 different ways screwing around with the quotes and backslashes that just confused me. Thanks.

0 Kudos
JoeBorgione
MVP Esteemed Contributor

Yep. 'Love me a good ol' over-thinking session.  Then when it's over, simple wins!

That should just about do it....
0 Kudos
DavidPike
MVP Frequent Contributor

What an interesting question, I was trying to wrap my head around that!

Can you just work out which domains contain 'Overhead'?

eg if overhead appears in coded value 2,4,9 and 10:

"[SUBTYPECD]" IN (2, 4 ,9, 10)

I also believe the query should be passed as a string in itself, ie:

query = ' "[SUBTYPECD]" IN (2, 4 ,9, 10) '

arcpy.SelectLayerByAttribute_management ("OHlyr3", "NEW_SELECTION", query ) 

hope this helps

JoeBorgione
MVP Esteemed Contributor

The beauty of this forum!  

That should just about do it....
0 Kudos