<?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: How to pass variable in where sql expression in Python in ArcGIS Online Questions</title>
    <link>https://community.esri.com/t5/arcgis-online-questions/how-to-pass-variable-in-where-sql-expression-in/m-p/1210061#M47736</link>
    <description>&lt;P&gt;Hello,&lt;/P&gt;&lt;P&gt;When you say that it is "this format", do you mean that it has the square brackets around it? If yes, we should get rid of those. Those brackets indicate a data type of a list. So your variable is a list with one item, the number you are after. We can keep that number and ditch the list holding it by referencing the number's position in the list: it is the first and only position in the list. We call this position an index. Indexes in Python always start at 0, not 1.&lt;/P&gt;&lt;P&gt;Can you post the part of your code where the variable tStart is defined? We will want to modify that part to make sure tStart holds the number itself, not a list with a number in it.&lt;/P&gt;</description>
    <pubDate>Tue, 06 Sep 2022 19:34:17 GMT</pubDate>
    <dc:creator>MikeLachance1</dc:creator>
    <dc:date>2022-09-06T19:34:17Z</dc:date>
    <item>
      <title>How to pass variable in where sql expression in Python</title>
      <link>https://community.esri.com/t5/arcgis-online-questions/how-to-pass-variable-in-where-sql-expression-in/m-p/1210037#M47734</link>
      <description>&lt;P&gt;I need to select records from a QuickCapture app that were created after a certain timestamp which is written to a feature service table each time the script runs - this is done in order to not append duplicates to the target feature class. I am attempting to pass my "tStamp" variable into 'where_clause' where I'm comparing it to CreationDate in SelectLayerByAttribute. Here's what I have at the moment:&lt;/P&gt;&lt;LI-CODE lang="c"&gt;QC_selection = arcpy.management.SelectLayerByAttribute(in_layer_or_view=QC_Feature_Layer, selection_type="NEW_SELECTION", where_clause = """'CreationDate' &amp;gt; timestamp '{0}'""".format(tStamp), invert_where_clause="NON_INVERT")&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;I've tried combinations of single and double quotes, curly braces, and brackets. I've tried storing the sql expression in a variable and passing that. I've tried formatted string literals syntax of f'{tStamp}' - all of which have thrown the error: 'invalid expression'&lt;/P&gt;&lt;P&gt;When I put CreateDate into a python print statement it returns this format: [1662486472000]. For testing I have a string and date type in my timestamp table. I've tried using both in the where clause with no luck.&lt;/P&gt;&lt;P&gt;I've also tried using arcgis.features.query in the Python API with no luck either. The error here is: 'where' parameter is invalid.&lt;/P&gt;&lt;P&gt;I feel like I've tried everything - anyone have an idea?&amp;nbsp; Thanks.&lt;/P&gt;</description>
      <pubDate>Tue, 06 Sep 2022 18:51:48 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-online-questions/how-to-pass-variable-in-where-sql-expression-in/m-p/1210037#M47734</guid>
      <dc:creator>jdaniel</dc:creator>
      <dc:date>2022-09-06T18:51:48Z</dc:date>
    </item>
    <item>
      <title>Re: How to pass variable in where sql expression in Python</title>
      <link>https://community.esri.com/t5/arcgis-online-questions/how-to-pass-variable-in-where-sql-expression-in/m-p/1210061#M47736</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;&lt;P&gt;When you say that it is "this format", do you mean that it has the square brackets around it? If yes, we should get rid of those. Those brackets indicate a data type of a list. So your variable is a list with one item, the number you are after. We can keep that number and ditch the list holding it by referencing the number's position in the list: it is the first and only position in the list. We call this position an index. Indexes in Python always start at 0, not 1.&lt;/P&gt;&lt;P&gt;Can you post the part of your code where the variable tStart is defined? We will want to modify that part to make sure tStart holds the number itself, not a list with a number in it.&lt;/P&gt;</description>
      <pubDate>Tue, 06 Sep 2022 19:34:17 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-online-questions/how-to-pass-variable-in-where-sql-expression-in/m-p/1210061#M47736</guid>
      <dc:creator>MikeLachance1</dc:creator>
      <dc:date>2022-09-06T19:34:17Z</dc:date>
    </item>
    <item>
      <title>Re: How to pass variable in where sql expression in Python</title>
      <link>https://community.esri.com/t5/arcgis-online-questions/how-to-pass-variable-in-where-sql-expression-in/m-p/1210249#M47753</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://community.esri.com/t5/user/viewprofilepage/user-id/420721"&gt;@jdaniel&lt;/a&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;If you open up your APRX and use the SelectByAttributes tool and add a query on the date field, check the SQL button (highlighted in yellow) and you will see the format that the SQL query needs to be in for a date field. Similar to below.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Clubdebambos_0-1662538587728.png" style="width: 400px;"&gt;&lt;img src="https://community.esri.com/t5/image/serverpage/image-id/50599i87F2AB129C13DC9C/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Clubdebambos_0-1662538587728.png" alt="Clubdebambos_0-1662538587728.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Your query will look similar to&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;"CreationDate &amp;gt; timestamp '{0} 00:00:00'".format(tStamp)&lt;/LI-CODE&gt;&lt;P&gt;Where tStamp represents the date as YYYY-MM-DD&lt;/P&gt;&lt;P&gt;or&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;"CreationDate &amp;gt; timestamp '{0}'".format(tStamp)&lt;/LI-CODE&gt;&lt;P&gt;Where tStamp represents the date and time as YYYY-MM-DD HH:MM:SS&lt;/P&gt;</description>
      <pubDate>Wed, 07 Sep 2022 08:20:11 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-online-questions/how-to-pass-variable-in-where-sql-expression-in/m-p/1210249#M47753</guid>
      <dc:creator>Clubdebambos</dc:creator>
      <dc:date>2022-09-07T08:20:11Z</dc:date>
    </item>
    <item>
      <title>Re: How to pass variable in where sql expression in Python</title>
      <link>https://community.esri.com/t5/arcgis-online-questions/how-to-pass-variable-in-where-sql-expression-in/m-p/1211189#M47798</link>
      <description>&lt;P&gt;Thanks, that was an excellent observation and it was part of the issue. However, I still could not get the where clause to operate correctly while comparing epoch time. I was able to solve my problem with comparing the OBJECTIDs instead of timestamps.&lt;/P&gt;</description>
      <pubDate>Fri, 09 Sep 2022 17:29:03 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-online-questions/how-to-pass-variable-in-where-sql-expression-in/m-p/1211189#M47798</guid>
      <dc:creator>jdaniel</dc:creator>
      <dc:date>2022-09-09T17:29:03Z</dc:date>
    </item>
    <item>
      <title>Re: How to pass variable in where sql expression in Python</title>
      <link>https://community.esri.com/t5/arcgis-online-questions/how-to-pass-variable-in-where-sql-expression-in/m-p/1211202#M47800</link>
      <description>&lt;P&gt;Yes, I believe you would have to convert your epoch time to the 'YYYY-MM-DD HH:MM:SS' format that SQL uses for a Select query.&lt;/P&gt;</description>
      <pubDate>Fri, 09 Sep 2022 17:35:38 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-online-questions/how-to-pass-variable-in-where-sql-expression-in/m-p/1211202#M47800</guid>
      <dc:creator>MikeLachance1</dc:creator>
      <dc:date>2022-09-09T17:35:38Z</dc:date>
    </item>
    <item>
      <title>Re: How to pass variable in where sql expression in Python</title>
      <link>https://community.esri.com/t5/arcgis-online-questions/how-to-pass-variable-in-where-sql-expression-in/m-p/1211383#M47814</link>
      <description>&lt;LI-CODE lang="c"&gt;where_clause = f"CreationDate &amp;gt; timestamp '{time_stamp}'"&lt;/LI-CODE&gt;&lt;P&gt;This is what ended up working after I came back to it. My datetime format was a little off, as well. Thanks.&lt;/P&gt;</description>
      <pubDate>Sat, 10 Sep 2022 19:19:58 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-online-questions/how-to-pass-variable-in-where-sql-expression-in/m-p/1211383#M47814</guid>
      <dc:creator>jdaniel</dc:creator>
      <dc:date>2022-09-10T19:19:58Z</dc:date>
    </item>
  </channel>
</rss>

