Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

I'm working on a script that involves an arcpy.select_analysis function. The problem I have is that when I run the script it produces an empty file. I've verified that the whole thing works when in the Select GUI, and produces the correct output, and then have even exported the SQL expression and copy/pasted it into the arcgis python window, but no dice.

Here is the script and warning in ArcGIS python window

Here is the table view showing that zip 10004 does exist in this field.]

I've zoomed the extent both way out and way in to see if that makes a difference, but it doesn't. Any advice would be much appreciated!

share|improve this question
    
Is ZIP an Integer or Text field? I'm guessing Text from the syntax that you have tried but want to confirm. –  PolyGeo Apr 21 '13 at 0:26

1 Answer 1

up vote 7 down vote accepted

Instead of:

"'ZIP' = '10004'"

try this:

'"ZIP" = ' + "'10004'"

The reason that your original expression gives an empty output is because the string 'ZIP' is not the same as (equal to) the string '10004'.

share|improve this answer
    
thanks a ton! Your explanation makes sense, but for edifying clarification, why then is that the format of the expression in the SQL GUI? –  David Meek Apr 21 '13 at 0:42
1  
In the SQL GUI the syntax is "ZIP" = '10004' but what you posted had single quotes around both field and value. I'm happy to admit that I find getting SQL syntax right with Python/ArcPy can prove finicky. –  PolyGeo Apr 21 '13 at 1:14
    
finicky indeed! I pasted the single quotes in as for some reason that's how it exported, even though "ZIP" = '10004'was what was in the GUI. Thanks again –  David Meek Apr 21 '13 at 1:53
3  
If you are doing a quick test/hack this would work fine. However, when running bigger scripts with multiple data sources (personal,file,SDE geodatabases,shapefiles), the SQL syntax can be hard to manage. Consider using the resources.arcgis.com/en/help/main/10.1/index.html#//…, AddFieldDelimiters, which adds field delimiters to a field name to allow for use in SQL expressions. –  Alex Tereshenkov Apr 21 '13 at 6:06

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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