<?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 whereClause in SearchCursor issue in ArcMap 10.6 in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/whereclause-in-searchcursor-issue-in-arcmap-10-6/m-p/618757#M48258</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;&lt;SPAN&gt;I am having a problem with whereClause in SearchCursor. Can anyone tell me what is the issue here?&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;When I use the first version without date everything works fine. When I add the date part. It fails with the error messages I pasted at the bottom:&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;&amp;gt;&amp;gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;whereClause =&amp;nbsp; &amp;nbsp;'STA_CODE = "' + code + '" AND FC IS NOT NULL AND FC &amp;lt;&amp;gt; 0'&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;whereClause =&amp;nbsp; 'SAMP_DATE &amp;lt; "2020/12/31" AND STA_CODE = "' + code + '" AND FC IS NOT NULL AND FC &amp;lt;&amp;gt; 0'&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;recs = arcpy.SearchCursor(samples, whereClause, "", "", "SAMP_DATE D")&lt;/P&gt;&lt;P&gt;&amp;gt;&amp;gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&amp;gt;&amp;gt;&amp;gt;&amp;gt;&amp;gt;------&lt;/P&gt;&lt;P&gt;Failed script ScriptMP...&lt;/P&gt;&lt;P&gt;Traceback (most recent call last):&lt;BR /&gt; File "C:\Users\mpalleti\Documents\PMR\SamplingStatistics\test6E.py", line 47, in &amp;lt;module&amp;gt;&lt;BR /&gt; recs = arcpy.SearchCursor(samples, whereClause, "", "", "SAMP_DATE D")&lt;BR /&gt; File "c:\program files (x86)\arcgis\desktop10.6\arcpy\arcpy\__init__.py", line 1186, in SearchCursor&lt;BR /&gt; return gp.searchCursor(dataset, where_clause, spatial_reference, fields, sort_fields)&lt;BR /&gt; File "c:\program files (x86)\arcgis\desktop10.6\arcpy\arcpy\geoprocessing\_base.py", line 361, in searchCursor&lt;BR /&gt; self._gp.SearchCursor(*gp_fixargs(args, True)))&lt;BR /&gt;RuntimeError: ERROR 999999: Error executing function.&lt;BR /&gt;An invalid SQL statement was used.&lt;BR /&gt;An invalid SQL statement was used.&lt;/P&gt;&lt;P&gt;Failed to execute (ScriptMP).&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Thu, 15 Oct 2020 01:04:22 GMT</pubDate>
    <dc:creator>MohanPalleti</dc:creator>
    <dc:date>2020-10-15T01:04:22Z</dc:date>
    <item>
      <title>whereClause in SearchCursor issue in ArcMap 10.6</title>
      <link>https://community.esri.com/t5/python-questions/whereclause-in-searchcursor-issue-in-arcmap-10-6/m-p/618757#M48258</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;&lt;SPAN&gt;I am having a problem with whereClause in SearchCursor. Can anyone tell me what is the issue here?&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;When I use the first version without date everything works fine. When I add the date part. It fails with the error messages I pasted at the bottom:&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;&amp;gt;&amp;gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;whereClause =&amp;nbsp; &amp;nbsp;'STA_CODE = "' + code + '" AND FC IS NOT NULL AND FC &amp;lt;&amp;gt; 0'&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;whereClause =&amp;nbsp; 'SAMP_DATE &amp;lt; "2020/12/31" AND STA_CODE = "' + code + '" AND FC IS NOT NULL AND FC &amp;lt;&amp;gt; 0'&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;recs = arcpy.SearchCursor(samples, whereClause, "", "", "SAMP_DATE D")&lt;/P&gt;&lt;P&gt;&amp;gt;&amp;gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&amp;gt;&amp;gt;&amp;gt;&amp;gt;&amp;gt;------&lt;/P&gt;&lt;P&gt;Failed script ScriptMP...&lt;/P&gt;&lt;P&gt;Traceback (most recent call last):&lt;BR /&gt; File "C:\Users\mpalleti\Documents\PMR\SamplingStatistics\test6E.py", line 47, in &amp;lt;module&amp;gt;&lt;BR /&gt; recs = arcpy.SearchCursor(samples, whereClause, "", "", "SAMP_DATE D")&lt;BR /&gt; File "c:\program files (x86)\arcgis\desktop10.6\arcpy\arcpy\__init__.py", line 1186, in SearchCursor&lt;BR /&gt; return gp.searchCursor(dataset, where_clause, spatial_reference, fields, sort_fields)&lt;BR /&gt; File "c:\program files (x86)\arcgis\desktop10.6\arcpy\arcpy\geoprocessing\_base.py", line 361, in searchCursor&lt;BR /&gt; self._gp.SearchCursor(*gp_fixargs(args, True)))&lt;BR /&gt;RuntimeError: ERROR 999999: Error executing function.&lt;BR /&gt;An invalid SQL statement was used.&lt;BR /&gt;An invalid SQL statement was used.&lt;/P&gt;&lt;P&gt;Failed to execute (ScriptMP).&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 15 Oct 2020 01:04:22 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/whereclause-in-searchcursor-issue-in-arcmap-10-6/m-p/618757#M48258</guid>
      <dc:creator>MohanPalleti</dc:creator>
      <dc:date>2020-10-15T01:04:22Z</dc:date>
    </item>
    <item>
      <title>Re: whereClause in SearchCursor issue in ArcMap 10.6</title>
      <link>https://community.esri.com/t5/python-questions/whereclause-in-searchcursor-issue-in-arcmap-10-6/m-p/618758#M48259</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi &lt;A href="https://community.esri.com/migrated-users/351477"&gt;Mohan Palleti&lt;/A&gt;‌,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Querying dates is bit of a special case, in that the correct way to format the where clause is dependent upon the flavour of SQL used in the database containing our data - and often whether on not the field values specify both date AND time, or only the date. Generally speaking, if the field values include a both date and time - then we'll want to provide both in our where clause expression, while only the date need be specified if that's all our field values contain.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;So for example if we're working with a SQL Server database where both date and time are specified in the field values, the syntax for your where_clause expression would be&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE class="language-python line-numbers"&gt;&lt;CODE&gt;whereClause &lt;SPAN class="operator token"&gt;=&lt;/SPAN&gt;  &lt;SPAN class="string token"&gt;"SAMP_DATE &amp;lt; '2020-12-31 23:59:59' AND STA_CODE = '"&lt;/SPAN&gt; &lt;SPAN class="operator token"&gt;+&lt;/SPAN&gt; code &lt;SPAN class="operator token"&gt;+&lt;/SPAN&gt; &lt;SPAN class="string token"&gt;"' AND FC IS NOT NULL AND FC &amp;lt;&amp;gt; 0"&lt;/SPAN&gt;‍‍&lt;SPAN class="line-numbers-rows"&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Whereas the same expression for a File Geodatabase - which uses a slightly different flavour of SQL - the correct expression syntax would be formatted as&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE class="language-python line-numbers"&gt;&lt;CODE&gt;whereClause &lt;SPAN class="operator token"&gt;=&lt;/SPAN&gt;  &lt;SPAN class="string token"&gt;"SAMP_DATE &amp;lt; date '2020-12-31 23:59:59' AND STA_CODE = '"&lt;/SPAN&gt; &lt;SPAN class="operator token"&gt;+&lt;/SPAN&gt; code &lt;SPAN class="operator token"&gt;+&lt;/SPAN&gt; &lt;SPAN class="string token"&gt;"' AND FC IS NOT NULL AND FC &amp;lt;&amp;gt; 0"&lt;/SPAN&gt;‍‍‍&lt;SPAN class="line-numbers-rows"&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;For a complete reference on how to format date and time expressions in SQL / where_clause expressions for all supported databases, check out&amp;nbsp;&lt;A class="link-titled" href="https://desktop.arcgis.com/en/arcmap/latest/map/working-with-layers/sql-reference-for-query-expressions-used-in-arcgis.htm#GUID-85C03D85-F4A5-48FC-8E8C-3F79919430DB" title="https://desktop.arcgis.com/en/arcmap/latest/map/working-with-layers/sql-reference-for-query-expressions-used-in-arcgis.htm#GUID-85C03D85-F4A5-48FC-8E8C-3F79919430DB"&gt;SQL reference for query expressions used in ArcGIS—ArcMap | Documentation&lt;/A&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Hope that helps!&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Cheers,&lt;/P&gt;&lt;P&gt;James&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 15 Oct 2020 01:52:26 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/whereclause-in-searchcursor-issue-in-arcmap-10-6/m-p/618758#M48259</guid>
      <dc:creator>Anonymous User</dc:creator>
      <dc:date>2020-10-15T01:52:26Z</dc:date>
    </item>
    <item>
      <title>Re: whereClause in SearchCursor issue in ArcMap 10.6</title>
      <link>https://community.esri.com/t5/python-questions/whereclause-in-searchcursor-issue-in-arcmap-10-6/m-p/618759#M48260</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Thank you James!&lt;/P&gt;&lt;P&gt;Your suggestion 'date' worked! I was completely lost to fix this.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Mohan Palleti&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 17 Oct 2020 15:25:32 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/whereclause-in-searchcursor-issue-in-arcmap-10-6/m-p/618759#M48260</guid>
      <dc:creator>MohanPalleti</dc:creator>
      <dc:date>2020-10-17T15:25:32Z</dc:date>
    </item>
  </channel>
</rss>

