<?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: Inconsistency in SQL expression usage? in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/inconsistency-in-sql-expression-usage/m-p/1327401#M68582</link>
    <description>&lt;P&gt;Oh, I see what you mean now - the formatting avoids the use of concatenation the way I have set up that expression. And yeah, I can see now its a matter of the data source and not ArcPy driving these differences.&lt;/P&gt;&lt;P&gt;Appreciate your help!&lt;/P&gt;</description>
    <pubDate>Mon, 11 Sep 2023 18:30:17 GMT</pubDate>
    <dc:creator>clt_cabq</dc:creator>
    <dc:date>2023-09-11T18:30:17Z</dc:date>
    <item>
      <title>Inconsistency in SQL expression usage?</title>
      <link>https://community.esri.com/t5/python-questions/inconsistency-in-sql-expression-usage/m-p/1326982#M68556</link>
      <description>&lt;P&gt;I have a process where I pull data from multiple sources, and one step along the way is to filter data to match a date range. To do this I calculate a start date and end date and then build those into a sql expression, however I've noticed the query expression has to be constructed slightly different between two uses, here's an example:&lt;/P&gt;&lt;LI-CODE lang="python"&gt;qry = "CREATED_DATE &amp;gt;= '" + startdate + "' AND CREATED_DATE &amp;lt;= '" + enddate + "'"
apd_lyr = arcpy.MakeFeatureLayer_management(crime_data,"apd_crimes", qry)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;but in another use of this same pattern code ends up looking like this, with the keyword 'timestamp' added:&lt;/P&gt;&lt;LI-CODE lang="python"&gt;qry = "REPORTDATE &amp;gt;= timestamp'" + startdate + "' AND REPORTDATE &amp;lt;= timestamp '" + enddate + "'"
raw_CED_dates = arcpy.MakeTableView_management(raw_CED,'ced_dates',qry)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;is there any particular reason these two query strings need to be constructed slightly differently, or is this just an oddity in ArcPy? Both queries are working against date fields (CREATED_DATE and REPORTDATE).&lt;/P&gt;</description>
      <pubDate>Fri, 08 Sep 2023 20:02:42 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/inconsistency-in-sql-expression-usage/m-p/1326982#M68556</guid>
      <dc:creator>clt_cabq</dc:creator>
      <dc:date>2023-09-08T20:02:42Z</dc:date>
    </item>
    <item>
      <title>Re: Inconsistency in SQL expression usage?</title>
      <link>https://community.esri.com/t5/python-questions/inconsistency-in-sql-expression-usage/m-p/1326995#M68557</link>
      <description>&lt;P&gt;You haven't specified the data source type, so it's difficult to know what the SQL syntax of the provider requires, and what is optional (in PostgreSQL, you'd cast the string with 'ISO_string'::timestamp).&lt;/P&gt;&lt;P&gt;String math is not best practice (slower and difficult to layout and debug).&amp;nbsp; Instead, you really ought to use &lt;FONT face="courier new,courier"&gt;"".format()&lt;/FONT&gt; or &lt;FONT face="courier new,courier"&gt;f""&lt;/FONT&gt; formatting.&lt;/P&gt;&lt;P&gt;- V&lt;/P&gt;</description>
      <pubDate>Fri, 08 Sep 2023 20:40:15 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/inconsistency-in-sql-expression-usage/m-p/1326995#M68557</guid>
      <dc:creator>VinceAngelo</dc:creator>
      <dc:date>2023-09-08T20:40:15Z</dc:date>
    </item>
    <item>
      <title>Re: Inconsistency in SQL expression usage?</title>
      <link>https://community.esri.com/t5/python-questions/inconsistency-in-sql-expression-usage/m-p/1327283#M68568</link>
      <description>&lt;P&gt;Vince, the first example is a feature class in an SDE geodatabase with a SQL Server backend, the other is a local file geodatabase table (not a feature class). Also, can you clarify your comment about string math and formatting strings?&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 11 Sep 2023 14:10:24 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/inconsistency-in-sql-expression-usage/m-p/1327283#M68568</guid>
      <dc:creator>clt_cabq</dc:creator>
      <dc:date>2023-09-11T14:10:24Z</dc:date>
    </item>
    <item>
      <title>Re: Inconsistency in SQL expression usage?</title>
      <link>https://community.esri.com/t5/python-questions/inconsistency-in-sql-expression-usage/m-p/1327363#M68577</link>
      <description>&lt;P&gt;If you have different data sources, then the format change is not just an ArcPy quirk -- It may just be what is required by the differing SQL providers. Even file geodatabase and shapefile are different.&lt;/P&gt;&lt;P&gt;Python treats all strings as immutable, so the expression &lt;STRONG&gt;&lt;FONT face="courier new,courier"&gt;s = 'a' + 'b' + 'c' + 'd'&lt;/FONT&gt;&lt;/STRONG&gt; generates seven objects through an alloc()/free() sequence (keeping only the last), which is slower than &lt;STRONG&gt;&lt;FONT face="courier new,courier"&gt;"{:s}{:s}{:s}{:s}".format('a','b','c','d')&lt;/FONT&gt;&lt;/STRONG&gt; . When you add in the frequent formatting mistakes caused by string math and failure of some objects to format as string correctly, it's easy to see why this "feature" of the language isn't encouraged (and why &lt;FONT face="courier new,courier"&gt;f""&lt;/FONT&gt; was added at Python 3).&lt;/P&gt;&lt;P&gt;Try it (noting that the FGDB string had a formatting error without the space between "timestamp" and the apostrophe)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="c"&gt;qry = f"REPORTDATE &amp;gt;= timestamp '{startdate}' AND REPORTDATE &amp;lt;= timestamp '{enddate}'"&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;- V&lt;/P&gt;</description>
      <pubDate>Mon, 11 Sep 2023 19:31:16 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/inconsistency-in-sql-expression-usage/m-p/1327363#M68577</guid>
      <dc:creator>VinceAngelo</dc:creator>
      <dc:date>2023-09-11T19:31:16Z</dc:date>
    </item>
    <item>
      <title>Re: Inconsistency in SQL expression usage?</title>
      <link>https://community.esri.com/t5/python-questions/inconsistency-in-sql-expression-usage/m-p/1327401#M68582</link>
      <description>&lt;P&gt;Oh, I see what you mean now - the formatting avoids the use of concatenation the way I have set up that expression. And yeah, I can see now its a matter of the data source and not ArcPy driving these differences.&lt;/P&gt;&lt;P&gt;Appreciate your help!&lt;/P&gt;</description>
      <pubDate>Mon, 11 Sep 2023 18:30:17 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/inconsistency-in-sql-expression-usage/m-p/1327401#M68582</guid>
      <dc:creator>clt_cabq</dc:creator>
      <dc:date>2023-09-11T18:30:17Z</dc:date>
    </item>
  </channel>
</rss>

