<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: SQL Expression help, Select_Analysis in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/sql-expression-help-select-analysis/m-p/327840#M25512</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Ahh, thanks for the detailed information Curtis.&amp;nbsp; I definitely do not want to provide the wrong information for anyone who reads this thread.&amp;nbsp; I never use personal gdb's anyways so maybe that's why I never get the errors when I set up my queries.&amp;nbsp; It would be helpful if they provided all this information in the help docs to avoid confusion like this.&amp;nbsp; &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I do like your idea of using the .format() in queries.&amp;nbsp; I have actually been doing that a little bit lately and it seems to clean things up a little better.&amp;nbsp; Thanks for your valuable insight!&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;EDIT:&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Field delimiters can be found in the SelectLayerByAttribute tool in the where clause.&amp;nbsp; &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;A href="http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#//001700000071000000"&gt;http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#//001700000071000000&lt;/A&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Thanks again Curtis!&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Thu, 07 Mar 2013 16:03:04 GMT</pubDate>
    <dc:creator>Anonymous User</dc:creator>
    <dc:date>2013-03-07T16:03:04Z</dc:date>
    <item>
      <title>SQL Expression help, Select_Analysis</title>
      <link>https://community.esri.com/t5/python-questions/sql-expression-help-select-analysis/m-p/327836#M25508</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;In my code below, I am attempting to select features that have meet this Criteria : SNAP_FLAG (this is a field) &amp;lt; 3. Then I want to export these selected features as a new feature class.&lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;# Import arcpy module
import arcpy, sys, traceback
arcpy.env.overwriteOutput = True
from os import path as p

 
arcpy.env.workspace = r"G:\ChrisGIS\PS_Steelhead\Work"
try:
&amp;nbsp;&amp;nbsp;&amp;nbsp; for ws in arcpy.ListWorkspaces("*", "FileGDB"):
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.env.workspace = ws
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; print '\n\nSearching in %s\n\n' %ws
#
# Define Variables
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; NB = p.join(ws, 'NB')
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; NB_out = p.join(ws, 'NB_Snapped')
#
# Figure out Query Statement enter here&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; query = '"SNAP_FLAG" &amp;lt; \'3\''
#
# Select
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.Select_analysis(NB, NB_out, query)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; print 'NB_Snapped created from %s'%NB


&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I then get this error :&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Error Info:&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;ERROR 999999: Error executing function.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;An invalid SQL statement was used.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;An invalid SQL statement was used. [NB]&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;The table was not found. [NB_Snapped]&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Failed to execute (Select).&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;ArcPy ERRORS:&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;ERROR 999999: Error executing function.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;An invalid SQL statement was used.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;An invalid SQL statement was used. [NB]&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;The table was not found. [NB_Snapped]&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Failed to execute (Select).&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Any ideas what I am doing wrong? Is it the actual output table variable that is messed up, or is it my query...&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 12 Dec 2021 16:20:44 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/sql-expression-help-select-analysis/m-p/327836#M25508</guid>
      <dc:creator>ChristopherClark1</dc:creator>
      <dc:date>2021-12-12T16:20:44Z</dc:date>
    </item>
    <item>
      <title>Re: SQL Expression help, Select_Analysis</title>
      <link>https://community.esri.com/t5/python-questions/sql-expression-help-select-analysis/m-p/327837#M25509</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;I believe the problem here is that your SNAP_FLAG field is probably an integer type?&amp;nbsp; If that is the case, the query could simply be:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;query = '"SNAP_FLAG" &amp;lt; 3'&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;OR&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;query = 'SNAP_FLAG &amp;lt; 3'&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;The help docs say that you need to always have double quotes around field names but I hardly ever do that anymore and it still works (may just be for shapefiles?).&amp;nbsp; Anyways, you only need the single quotes after an operator (AND, OR, &amp;lt;&amp;gt;, = etc) if it is a text string type.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;For example:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;"CITY = 'CHICAGO'"&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;You would need single quotes around the string CHICAGO.&amp;nbsp; Hope this helps.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 06 Mar 2013 20:44:04 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/sql-expression-help-select-analysis/m-p/327837#M25509</guid>
      <dc:creator>Anonymous User</dc:creator>
      <dc:date>2013-03-06T20:44:04Z</dc:date>
    </item>
    <item>
      <title>Re: SQL Expression help, Select_Analysis</title>
      <link>https://community.esri.com/t5/python-questions/sql-expression-help-select-analysis/m-p/327838#M25510</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Yep! That was the answer. I was using the Arc Help Docs, and that is where I got that goofy syntax. Thanks for helping out.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 06 Mar 2013 20:49:35 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/sql-expression-help-select-analysis/m-p/327838#M25510</guid>
      <dc:creator>ChristopherClark1</dc:creator>
      <dc:date>2013-03-06T20:49:35Z</dc:date>
    </item>
    <item>
      <title>Re: SQL Expression help, Select_Analysis</title>
      <link>https://community.esri.com/t5/python-questions/sql-expression-help-select-analysis/m-p/327839#M25511</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Caleb, you inspired me to drop some info about SQL expressions in arcpy scripts into this thread...&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BLOCKQUOTE class="jive-quote"&gt;The help docs say that you need to always have double quotes around field names but I hardly ever do that anymore and it still works (may just be for shapefiles?).&amp;nbsp; Anyways, you only need the single quotes after an operator (AND, OR, &amp;lt;&amp;gt;, = etc) if it is a text string type.&lt;/BLOCKQUOTE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Yes, single quotes are required in SQL for all string literals.&amp;nbsp; &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Double quotes are needed in SQL to protect your field name from parsing, for example, to make sure reserved words or embedded operators in field names (for example:&amp;nbsp; "COUNT"; "FIELD-1") do not break your SQL expression. That's why you often see double-quotes used in examples -- it's best practice just in case your field names are not legal. This is especially important if your field name came in as a tool parameter (you as the script developer can't control what people will try to do!).&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Personal geodatabase and some ODBC-sourced tables &lt;/SPAN&gt;&lt;STRONG style="text-decoration: underline;"&gt;always&lt;/STRONG&gt;&lt;SPAN&gt; require brackets around field names: &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;
[CITY] = 'CHICAGO'
&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;There is an arcpy method that will automatically add the appropriate field delimiter based on the workspace, just in case someone decides to ignore or can't take your good advice to avoid personal GDB's. Using this method will keep your script from breaking.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;
&amp;gt;&amp;gt;&amp;gt;&amp;gt; print arcpy.AddFieldDelimiters("C:\\work\\mypersonal.mdb","CITY")
[CITY]
&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Last but not least, I want to encourage using substitution SQL expressions, as they make dealing with all this easier:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;
field = "CITY"
cityname = "Chicago"
wks = "C:/Data/MyWorkspace.mdb"
where_expr = "{0} = '{1}'".format(arcpy.AddFieldDelimiters(wks,field),cityName)
&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 15:32:26 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/sql-expression-help-select-analysis/m-p/327839#M25511</guid>
      <dc:creator>curtvprice</dc:creator>
      <dc:date>2021-12-11T15:32:26Z</dc:date>
    </item>
    <item>
      <title>Re: SQL Expression help, Select_Analysis</title>
      <link>https://community.esri.com/t5/python-questions/sql-expression-help-select-analysis/m-p/327840#M25512</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Ahh, thanks for the detailed information Curtis.&amp;nbsp; I definitely do not want to provide the wrong information for anyone who reads this thread.&amp;nbsp; I never use personal gdb's anyways so maybe that's why I never get the errors when I set up my queries.&amp;nbsp; It would be helpful if they provided all this information in the help docs to avoid confusion like this.&amp;nbsp; &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I do like your idea of using the .format() in queries.&amp;nbsp; I have actually been doing that a little bit lately and it seems to clean things up a little better.&amp;nbsp; Thanks for your valuable insight!&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;EDIT:&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Field delimiters can be found in the SelectLayerByAttribute tool in the where clause.&amp;nbsp; &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;A href="http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#//001700000071000000"&gt;http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#//001700000071000000&lt;/A&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Thanks again Curtis!&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 07 Mar 2013 16:03:04 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/sql-expression-help-select-analysis/m-p/327840#M25512</guid>
      <dc:creator>Anonymous User</dc:creator>
      <dc:date>2013-03-07T16:03:04Z</dc:date>
    </item>
  </channel>
</rss>

