<?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: Odd SelectLayerByAttribute error concerning SQL clause..... in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/odd-selectlayerbyattribute-error-concerning-sql/m-p/413988#M32601</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Update 2.... it looks like the difference is the original feature_layer object has an SDE/Oracle source, where the LENGTH and TRIM functions are valid within SQL statements. The tmplyr which originally was populated via a CopyFeatures action was stored in a file geodatabase source, which does not accept LENGHT and TRIM within the SQL.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Tue, 30 Sep 2014 15:24:52 GMT</pubDate>
    <dc:creator>PeterLen</dc:creator>
    <dc:date>2014-09-30T15:24:52Z</dc:date>
    <item>
      <title>Odd SelectLayerByAttribute error concerning SQL clause.....</title>
      <link>https://community.esri.com/t5/python-questions/odd-selectlayerbyattribute-error-concerning-sql/m-p/413981#M32594</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hello - We are using ArcGIS 10.0 and I and using arcpy (Python 2.6) to do some queries. This is what I have:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE __default_attr="python" __jive_macro_name="code" class="jive_macro_code jive_text_macro _jivemacro_uid_14120335815734125" jivemacro_uid="_14120335815734125" modifiedtitle="true"&gt;
&lt;P&gt;# Do a location search based on an extent&lt;/P&gt;
&lt;P&gt;feature_layer = "in_memory\\featlyr"&lt;/P&gt;
&lt;P&gt;arcpy.MakeFeatureLayer_management(fc, feature_layer)&lt;/P&gt;
&lt;P&gt;arcpy.SelectLayerByLocation_management(feature_layer, "INTERSECT", AOI, "", "NEW_SELECTION")&lt;/P&gt;
&lt;P&gt;&lt;/P&gt;
&lt;P&gt;&lt;/P&gt;
&lt;P&gt;# Take the results and add them to a new layer since dealing with the feature layer is time consuming since&lt;/P&gt;
&lt;P&gt;#&amp;nbsp; the total number of features is large&lt;/P&gt;
&lt;P&gt;tmpfc = "in_memory\\tmpfc"&lt;/P&gt;
&lt;P&gt;arcpy.CopyFeatures_management(feature_layer, tmpfc)&lt;/P&gt;
&lt;P&gt;tmplyr = "in_memory\\tmplyr"&lt;/P&gt;
&lt;P&gt;arcpy.MakeFeatureLayer_management(tmpfc, tmplyr)&lt;/P&gt;
&lt;P&gt;&lt;/P&gt;
&lt;P&gt;&lt;/P&gt;
&lt;P&gt;# Go through a loop to do attribute queries based on the initially selected feature.&amp;nbsp; This will essentially be a&lt;/P&gt;
&lt;P&gt;#&amp;nbsp; loop for sub-queries but do not want to SUBSET_SELECTION with the feature_layer because there will be&lt;/P&gt;
&lt;P&gt;#&amp;nbsp; issues concerning clearing the layer for the next sub-query.&lt;/P&gt;
&lt;P&gt;for ......:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; sql = "PRODUCER like 'A%' and (SERIES_ID is&amp;nbsp; null or LENGTH(TRIM(SERIES_ID)) = 0) and SCALE &amp;gt; 75000"&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; arcpy.SelectLayerByAttribute_management(tmplyr, "NEW_SELECTION", sql)&amp;nbsp; # Error occurs here&lt;/P&gt;



&lt;/PRE&gt;&lt;PRE __default_attr="python" __jive_macro_name="code" class="jive_macro_code jive_text_macro _jivemacro_uid_14120335245231343" jivemacro_uid="_14120335245231343"&gt;
&lt;P&gt;&lt;/P&gt;



&lt;/PRE&gt;&lt;PRE __default_attr="python" __jive_macro_name="code" class="jive_macro_code jive_text_macro _jivemacro_uid_14120333692725456" jivemacro_uid="_14120333692725456"&gt;
&lt;P&gt;&lt;/P&gt;



&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Looks like some got cut off due to the code highlighting.&amp;nbsp; Here is the full SQL:&lt;/P&gt;&lt;P&gt;sql = "PRODUCER like 'A%' and (SERIES_ID is null or LENGTH(TRIM(SERIES_ID)) = 0) and SCALE &amp;gt; 75000"&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;The error that I encounter is an "Invalid expression" error.&amp;nbsp; The error is due to the "LENGTH(TRIM(SERIES_ID))" part of the SQL.&amp;nbsp; Now, if I use my feature_layer as my argument to the arcpy.SelectLayerByAttribute_management call, there is no error and everyting runs fine.&amp;nbsp; I only get the error when I use the SQL syntax with my tmplyr.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I don't know what is different in the feature_layer that does not cause the error, as compared with the tmplyr.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Any thoughts?&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Thanks - Peter&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 29 Sep 2014 18:27:55 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/odd-selectlayerbyattribute-error-concerning-sql/m-p/413981#M32594</guid>
      <dc:creator>PeterLen</dc:creator>
      <dc:date>2014-09-29T18:27:55Z</dc:date>
    </item>
    <item>
      <title>Re: Odd SelectLayerByAttribute error concerning SQL clause.....</title>
      <link>https://community.esri.com/t5/python-questions/odd-selectlayerbyattribute-error-concerning-sql/m-p/413982#M32595</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Can you edit your question and use the advance editor option (top right) then &amp;gt;&amp;gt; button to set syntax highlighting, it makes code a lot easier to read...&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 29 Sep 2014 18:43:56 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/odd-selectlayerbyattribute-error-concerning-sql/m-p/413982#M32595</guid>
      <dc:creator>DuncanHornby</dc:creator>
      <dc:date>2014-09-29T18:43:56Z</dc:date>
    </item>
    <item>
      <title>Re: Odd SelectLayerByAttribute error concerning SQL clause.....</title>
      <link>https://community.esri.com/t5/python-questions/odd-selectlayerbyattribute-error-concerning-sql/m-p/413983#M32596</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;"LENGTH(TRIM(SERIES_ID))" just doesn't make sense.&amp;nbsp; Can you express the full query you wish to apply?&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I can see two potential issues:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;1.&amp;nbsp; You are not evaluating any specific field.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;SELECT * FROM &amp;lt;layer_name&amp;gt; WHERE &lt;STRONG&gt;&amp;lt;field name&amp;gt;&lt;/STRONG&gt; = 0&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;2. TRIM(SERIES_ID) I suspect that SERIES_ID is an integer value being represented as text (hence the need to issue TRIM).&amp;nbsp; If so, then I'd recommend you actually fix the datasource --- if it's actually integers you need to store, then set the field to that type.&amp;nbsp; Then there is no gymnastics required to TRIM strings or worry about other such issues because the data is correct.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 29 Sep 2014 19:48:12 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/odd-selectlayerbyattribute-error-concerning-sql/m-p/413983#M32596</guid>
      <dc:creator>JamesCrandall</dc:creator>
      <dc:date>2014-09-29T19:48:12Z</dc:date>
    </item>
    <item>
      <title>Re: Odd SelectLayerByAttribute error concerning SQL clause.....</title>
      <link>https://community.esri.com/t5/python-questions/odd-selectlayerbyattribute-error-concerning-sql/m-p/413984#M32597</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Duncan - Thanks for the heads up about the syntax highlighting.&amp;nbsp; It cut a little of it off but it is easier to read.&amp;nbsp; I added the full SQL syntax to help.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 29 Sep 2014 23:37:07 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/odd-selectlayerbyattribute-error-concerning-sql/m-p/413984#M32597</guid>
      <dc:creator>PeterLen</dc:creator>
      <dc:date>2014-09-29T23:37:07Z</dc:date>
    </item>
    <item>
      <title>Re: Odd SelectLayerByAttribute error concerning SQL clause.....</title>
      <link>https://community.esri.com/t5/python-questions/odd-selectlayerbyattribute-error-concerning-sql/m-p/413985#M32598</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;James - Thanks for the reply.&amp;nbsp; The SERIES ID is a string field even though most values are straight numbers.&amp;nbsp; Some do have alpha characters so the column had to be string-based.&amp;nbsp; I added an example of the full SQL to give you a better idea.&amp;nbsp; Again, the SQL does not produce an error when using the feature_layer but it does when using the tmplyr which I traced to the use of the functions LENGTH and TRIM.&amp;nbsp; Not sure what the difference is except feature_layer contains all 500K + features, including the selected ones from the location search.&amp;nbsp; The tmplyr just has the selected features.&amp;nbsp; If I simply state SERIES_ID = '', then there is no error, but I can't use that since some values do have spaces for values (it is not my data to clean up).&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Thank s- Peter&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 29 Sep 2014 23:42:19 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/odd-selectlayerbyattribute-error-concerning-sql/m-p/413985#M32598</guid>
      <dc:creator>PeterLen</dc:creator>
      <dc:date>2014-09-29T23:42:19Z</dc:date>
    </item>
    <item>
      <title>Re: Odd SelectLayerByAttribute error concerning SQL clause.....</title>
      <link>https://community.esri.com/t5/python-questions/odd-selectlayerbyattribute-error-concerning-sql/m-p/413986#M32599</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;In many SQL versions the syntax should be this (the "IS" is required with the keyword NULL):&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE __default_attr="sql" __jive_macro_name="code" class="jive_macro_code _jivemacro_uid_1412048142888949 jive_text_macro" jivemacro_uid="_1412048142888949"&gt;
&lt;P&gt;SERIES_ID IS NOT NULL&lt;/P&gt;
&lt;/PRE&gt;&lt;P&gt;or&lt;/P&gt;&lt;PRE __default_attr="sql" __jive_macro_name="code" class="jive_macro_code jive_text_macro _jivemacro_uid_14120481589619638" jivemacro_uid="_14120481589619638"&gt;
&lt;P&gt;NOT (SERIES_ID IS NULL)&lt;/P&gt;
&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Just an aside, in Python you can enter long strings using parentheses like this:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE __default_attr="python" __jive_macro_name="code" class="jive_macro_code _jivemacro_uid_14120481220838587 jive_text_macro" jivemacro_uid="_14120481220838587" modifiedtitle="true"&gt;
&lt;P&gt;sql = (&lt;SPAN class="string"&gt;"PRODUCER LIKE 'A%' AND "&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN class="string"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; "(SERIES_ID IS NOT NULL OR LENGTH(TRIM(SERIES_ID)) = 0) AND "&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN class="string"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; "SCALE &amp;gt; 75000") &lt;/SPAN&gt;&lt;/P&gt;
&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 30 Sep 2014 03:38:52 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/odd-selectlayerbyattribute-error-concerning-sql/m-p/413986#M32599</guid>
      <dc:creator>curtvprice</dc:creator>
      <dc:date>2014-09-30T03:38:52Z</dc:date>
    </item>
    <item>
      <title>Re: Odd SelectLayerByAttribute error concerning SQL clause.....</title>
      <link>https://community.esri.com/t5/python-questions/odd-selectlayerbyattribute-error-concerning-sql/m-p/413987#M32600</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Update....... it appears the problem is related to copying the selected features to an in-memory feature class and then making a lyer off of that class:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;UL&gt;&lt;LI&gt;tmpfc = &lt;SPAN class="string"&gt;"in_memory\\tmpfc"&amp;nbsp; &lt;/SPAN&gt;&lt;/LI&gt;&lt;LI&gt;arcpy.CopyFeatures_management(feature_layer, tmpfc)&amp;nbsp; &lt;/LI&gt;&lt;LI&gt;tmplyr = &lt;SPAN class="string"&gt;"in_memory\\tmplyr"&amp;nbsp; &lt;/SPAN&gt;&lt;/LI&gt;&lt;LI&gt;arcpy.MakeFeatureLayer_management(tmpfc, tmplyr)&amp;nbsp; &lt;/LI&gt;&lt;/UL&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;The tmplyr object, albeit a feature layer like the original feature_layer object is, is really not the same type of layer.&amp;nbsp; I essentially did this process via ArcMap and the toolbox.&amp;nbsp; When I look at the original feature_layer attribute table, the SERIES_ID column has an asterisk next to it and the column names in the "Select by Attributes" dialog appear without quotes, like OBJECTID, ITEM_ID, SERIES_ID.&amp;nbsp; When I look at the tmplyr attribute table, the SERIES_ID column does not have an asterisk and the column names in the "Select by Attributes" dialog contains quotes like, "OBJECTID", "ITEM_ID", "SERIES_ID".&amp;nbsp; something is obviously different.&amp;nbsp; When i try to run my SQL in the "Select by Attributes" dialog I get the error when using the tmplyr but not the orignal feature_layer, just as my Python results had.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 30 Sep 2014 15:16:05 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/odd-selectlayerbyattribute-error-concerning-sql/m-p/413987#M32600</guid>
      <dc:creator>PeterLen</dc:creator>
      <dc:date>2014-09-30T15:16:05Z</dc:date>
    </item>
    <item>
      <title>Re: Odd SelectLayerByAttribute error concerning SQL clause.....</title>
      <link>https://community.esri.com/t5/python-questions/odd-selectlayerbyattribute-error-concerning-sql/m-p/413988#M32601</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Update 2.... it looks like the difference is the original feature_layer object has an SDE/Oracle source, where the LENGTH and TRIM functions are valid within SQL statements. The tmplyr which originally was populated via a CopyFeatures action was stored in a file geodatabase source, which does not accept LENGHT and TRIM within the SQL.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 30 Sep 2014 15:24:52 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/odd-selectlayerbyattribute-error-concerning-sql/m-p/413988#M32601</guid>
      <dc:creator>PeterLen</dc:creator>
      <dc:date>2014-09-30T15:24:52Z</dc:date>
    </item>
    <item>
      <title>Re: Odd SelectLayerByAttribute error concerning SQL clause.....</title>
      <link>https://community.esri.com/t5/python-questions/odd-selectlayerbyattribute-error-concerning-sql/m-p/413989#M32602</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Not sure about TRIM, but you might try LEN as an alternative for LENGTH.&lt;/P&gt;&lt;P&gt;edit: for TRIM, maybe &lt;A href="https://www.udemy.com/blog/trim-in-sql/"&gt;this page&lt;/A&gt; would help.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 30 Sep 2014 17:14:46 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/odd-selectlayerbyattribute-error-concerning-sql/m-p/413989#M32602</guid>
      <dc:creator>Zeke</dc:creator>
      <dc:date>2014-09-30T17:14:46Z</dc:date>
    </item>
    <item>
      <title>Re: Odd SelectLayerByAttribute error concerning SQL clause.....</title>
      <link>https://community.esri.com/t5/python-questions/odd-selectlayerbyattribute-error-concerning-sql/m-p/413990#M32603</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;LTRIM() or RTRIM() would be what to use.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Try:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE __default_attr="sql" __jive_macro_name="code" class="jive_macro_code jive_text_macro _jivemacro_uid_14121038593189153" jivemacro_uid="_14121038593189153"&gt;
&lt;P&gt;sql = &lt;SPAN class="string"&gt;"PRODUCER like 'A%' and (SERIES_ID is&amp;nbsp; null or LEN(LTRIM(RTRIM(SERIES_ID))) = 0)&lt;/SPAN&gt;&lt;/P&gt;
&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 30 Sep 2014 19:02:25 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/odd-selectlayerbyattribute-error-concerning-sql/m-p/413990#M32603</guid>
      <dc:creator>JamesCrandall</dc:creator>
      <dc:date>2014-09-30T19:02:25Z</dc:date>
    </item>
    <item>
      <title>Re: Odd SelectLayerByAttribute error concerning SQL clause.....</title>
      <link>https://community.esri.com/t5/python-questions/odd-selectlayerbyattribute-error-concerning-sql/m-p/413991#M32604</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hmmmm.... I thought that might be it, but when I tried those, I still get the error.&amp;nbsp; I am totally outside of my application now and working with the layers in an ArcMap example.&amp;nbsp; For the file GDB layer, I took everything out of the SQl except for things like LEN(SERIES_ID) = 0 as well as LTRIM(SERIES_ID) = '', but both produce the error.&amp;nbsp; I'll keep looking in that vein though.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 30 Sep 2014 19:36:30 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/odd-selectlayerbyattribute-error-concerning-sql/m-p/413991#M32604</guid>
      <dc:creator>PeterLen</dc:creator>
      <dc:date>2014-09-30T19:36:30Z</dc:date>
    </item>
    <item>
      <title>Re: Odd SelectLayerByAttribute error concerning SQL clause.....</title>
      <link>https://community.esri.com/t5/python-questions/odd-selectlayerbyattribute-error-concerning-sql/m-p/413992#M32605</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;The help page with the details as to what the file gdb supports is here:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;A href="http://resources.arcgis.com/en/help/main/10.2/index.html#/SQL_reference_for_query_expressions_used_in_ArcGIS/00s500000033000000/" title="http://resources.arcgis.com/en/help/main/10.2/index.html#/SQL_reference_for_query_expressions_used_in_ArcGIS/00s500000033000000/"&gt;ArcGIS Help (10.2, 10.2.1, and 10.2.2): SQL&lt;/A&gt;&lt;A href="http://resources.arcgis.com/en/help/main/10.2/index.html#/SQL_reference_for_query_expressions_used_in_ArcGIS/00s500000033000000/"&gt; Expressions for query expressions used in ArcGIS.&lt;/A&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;TRIM (with options for left, right, and both trims)&amp;nbsp; and CHAR_LENGTH are both supported.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Also -- string comparison results can vary between data sources as much as the SQL syntax does - so I strongly suggest doing a unique tabulation of your values and testing them interactively in table view in ArcMap.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 01 Oct 2014 00:45:58 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/odd-selectlayerbyattribute-error-concerning-sql/m-p/413992#M32605</guid>
      <dc:creator>curtvprice</dc:creator>
      <dc:date>2014-10-01T00:45:58Z</dc:date>
    </item>
    <item>
      <title>Re: Odd SelectLayerByAttribute error concerning SQL clause.....</title>
      <link>https://community.esri.com/t5/python-questions/odd-selectlayerbyattribute-error-concerning-sql/m-p/413993#M32606</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I guess I don't understand why you do not actually fix the underlying problem.&amp;nbsp; You have identified a need to issue TRIM(), so why don't you fix the datasource now and eliminate the need to perform this in your SQL?&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Also, print out the field names for the in_memory fc's your are generating.&amp;nbsp; Perhaps the query fails because you are not stating the correct fields?&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 01 Oct 2014 12:08:20 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/odd-selectlayerbyattribute-error-concerning-sql/m-p/413993#M32606</guid>
      <dc:creator>JamesCrandall</dc:creator>
      <dc:date>2014-10-01T12:08:20Z</dc:date>
    </item>
    <item>
      <title>Re: Odd SelectLayerByAttribute error concerning SQL clause.....</title>
      <link>https://community.esri.com/t5/python-questions/odd-selectlayerbyattribute-error-concerning-sql/m-p/413994#M32607</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Thanks for all the replies guys.&amp;nbsp; I am sure it is just a matter of syntax for the file GDB type.&amp;nbsp; As for the fixing the data rather than mucking around with this syntax issue, we don't own the data or even the SQL syntax.&amp;nbsp; The various SQL items (which I loop though) are used to perform sub-queries on the original location query.&amp;nbsp;&amp;nbsp; Someone else owns the data we are querying as well as the SQL statements that we use so I have no flexibility in fixing any data issues.&amp;nbsp; If I can find the right syntax I will still have to do some Python string replacement.&amp;nbsp; It is on the backburner for now, but I will get back to it.&amp;nbsp; Thanks - Peter &lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 01 Oct 2014 12:32:39 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/odd-selectlayerbyattribute-error-concerning-sql/m-p/413994#M32607</guid>
      <dc:creator>PeterLen</dc:creator>
      <dc:date>2014-10-01T12:32:39Z</dc:date>
    </item>
    <item>
      <title>Re: Odd SelectLayerByAttribute error concerning SQL clause.....</title>
      <link>https://community.esri.com/t5/python-questions/odd-selectlayerbyattribute-error-concerning-sql/m-p/413995#M32608</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;PRE __default_attr="python" __jive_macro_name="code" class="jive_macro_code _jivemacro_uid_14121687178698520 jive_text_macro" jivemacro_uid="_14121687178698520"&gt;
&lt;P&gt;&lt;/P&gt;
&lt;/PRE&gt;&lt;P&gt;Then I would seriously question the validity of the data you are being provided.&amp;nbsp; If the provider cannot or will not perform a simple process (like removing leading/trailing spaces in a string field), then I would have some pause as to the correctness of the data in its entirety.&amp;nbsp; Anytime a developer has to perform silly gymnastics is only compounding the potential problems and potential incorrect, inconsistent and overall poor output/results.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;With that said, I would suggest to go back to your OP and simplify the query to bare-bones minimum.&amp;nbsp; For example I'd take this:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE __default_attr="python" __jive_macro_name="code" class="jive_macro_code jive_text_macro _jivemacro_uid_14121687250318246" jivemacro_uid="_14121687250318246"&gt;
&lt;P&gt;sql = "PRODUCER like 'A%' and (SERIES_ID is&amp;nbsp; null or LENGTH(TRIM(SERIES_ID)) = 0) and SCALE &amp;gt; 75000"&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/P&gt;
&lt;P&gt;arcpy.SelectLayerByAttribute_management(tmplyr, "NEW_SELECTION", sql)&lt;/P&gt;
&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;and try this:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE __default_attr="python" __jive_macro_name="code" class="jive_macro_code jive_text_macro _jivemacro_uid_1412168597333197" jivemacro_uid="_1412168597333197"&gt;
&lt;P&gt;sql = "PRODUCER like 'A%'"&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/P&gt;
&lt;P&gt;arcpy.SelectLayerByAttribute_management(tmplyr, "NEW_SELECTION", sql) &lt;/P&gt;

&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Does it work?&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Yes?&amp;nbsp; Then expand it a bit:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE __default_attr="python" __jive_macro_name="code" class="jive_macro_code jive_text_macro _jivemacro_uid_14121685842966141" jivemacro_uid="_14121685842966141"&gt;
&lt;P&gt;sql = "PRODUCER like 'A%' AND SCALE &amp;gt; 75000"&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/P&gt;
&lt;P&gt;arcpy.SelectLayerByAttribute_management(tmplyr, "NEW_SELECTION", sql) &lt;/P&gt;

&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;No?&amp;nbsp; Then something basic like "PRODUCER" is not actually a field in your in_memory fc.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 01 Oct 2014 13:03:44 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/odd-selectlayerbyattribute-error-concerning-sql/m-p/413995#M32608</guid>
      <dc:creator>JamesCrandall</dc:creator>
      <dc:date>2014-10-01T13:03:44Z</dc:date>
    </item>
    <item>
      <title>Re: Odd SelectLayerByAttribute error concerning SQL clause.....</title>
      <link>https://community.esri.com/t5/python-questions/odd-selectlayerbyattribute-error-concerning-sql/m-p/413996#M32609</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I just discovered that some SQL statements that work just fine on a GDB do not work on in_memory datasets.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;My issue was CAST.&amp;nbsp; It works just fine in ArcMap and when I hit a GDB directly.&amp;nbsp; But with a in_memory it says invalid SQL statement.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I found this post where Esri says they are different but do not give any doc or hints as to what is and what is not supported.&amp;nbsp; &lt;A _jive_internal="true" href="https://community.esri.com/thread/165846"&gt;https://community.esri.com/thread/165846&lt;/A&gt;&amp;nbsp; Please Esri document this stuff!&amp;nbsp; It costs lots of hours.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Thanks&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 18 Jul 2018 17:16:46 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/odd-selectlayerbyattribute-error-concerning-sql/m-p/413996#M32609</guid>
      <dc:creator>DougBrowning</dc:creator>
      <dc:date>2018-07-18T17:16:46Z</dc:date>
    </item>
  </channel>
</rss>

