<?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 set up where clause for date when extracting data from a map service using python in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/how-to-set-up-where-clause-for-date-when/m-p/1185606#M64809</link>
    <description>&lt;P&gt;Depending on the service / DB being queried, the timestamp needs to be formatted a particular way. Try something like&amp;nbsp;&lt;STRONG&gt;timestampe '&amp;lt;your timestamp here&amp;gt;'&lt;/STRONG&gt; and see if that works.&lt;/P&gt;&lt;P&gt;Also, you may want to check out the ArcGIS Python API. It's purpose-built for interacting with GIS portals and servers, and I personally find it easier for querying services than using requests.&lt;/P&gt;</description>
    <pubDate>Thu, 23 Jun 2022 17:03:51 GMT</pubDate>
    <dc:creator>jcarlson</dc:creator>
    <dc:date>2022-06-23T17:03:51Z</dc:date>
    <item>
      <title>How to set up where clause for date when extracting data from a map service using python</title>
      <link>https://community.esri.com/t5/python-questions/how-to-set-up-where-clause-for-date-when/m-p/1185588#M64808</link>
      <description>&lt;P&gt;I successfully used Python 3 to download data from a REST URL using the information from this link&amp;nbsp;&lt;A href="https://support.esri.com/en/technical-article/000019645" target="_blank"&gt;How To: Extract data from a map service using Python (esri.com)&lt;/A&gt;.&lt;/P&gt;&lt;P&gt;I need help setting up my where clause when using a Date field.&lt;/P&gt;&lt;P&gt;Here is what worked when I used a non date field in my where clause:&lt;/P&gt;&lt;P&gt;params = {'where': 'RoadNumber &amp;lt; "00200"',&lt;BR /&gt;'geometryType': 'esriGeometryEnvelope',&lt;BR /&gt;'spatialRel': 'esriSpatialRelIntersects',&lt;BR /&gt;'relationParam': '',&lt;BR /&gt;'outFields': '*',&lt;BR /&gt;'returnGeometry': 'true',&lt;BR /&gt;'geometryPrecision':'',&lt;BR /&gt;'outSR': '',&lt;BR /&gt;'returnIdsOnly': 'false',&lt;BR /&gt;'returnCountOnly': 'false',&lt;BR /&gt;'orderByFields': '',&lt;BR /&gt;'groupByFieldsForStatistics': '',&lt;BR /&gt;'returnZ': 'false',&lt;BR /&gt;'returnM': 'false',&lt;BR /&gt;'returnDistinctValues': 'false',&lt;BR /&gt;'f': 'pjson',&lt;BR /&gt;'token': token&lt;BR /&gt;}&lt;/P&gt;&lt;P&gt;These don't work in my where clause for a date:&lt;/P&gt;&lt;P&gt;# Find today's date and calculate last 5 years&lt;BR /&gt;today = date.today()&lt;BR /&gt;thisyear = today.year&lt;BR /&gt;last5years = thisyear - 5&lt;BR /&gt;last5yeardate_string = str(last5years) + "-01-01 + 00:00:00"&lt;/P&gt;&lt;P&gt;params = {'where': 'CollisionDate &amp;gt;= last5yeardate_string',&amp;nbsp;&lt;/P&gt;&lt;P&gt;params = {'where': 'CollisionDate &amp;gt;= (datetime.datetime.now() - dateutil.relativedelta(years=5))',&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 23 Jun 2022 16:30:11 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/how-to-set-up-where-clause-for-date-when/m-p/1185588#M64808</guid>
      <dc:creator>LoriEmerson_McCormack</dc:creator>
      <dc:date>2022-06-23T16:30:11Z</dc:date>
    </item>
    <item>
      <title>Re: How to set up where clause for date when extracting data from a map service using python</title>
      <link>https://community.esri.com/t5/python-questions/how-to-set-up-where-clause-for-date-when/m-p/1185606#M64809</link>
      <description>&lt;P&gt;Depending on the service / DB being queried, the timestamp needs to be formatted a particular way. Try something like&amp;nbsp;&lt;STRONG&gt;timestampe '&amp;lt;your timestamp here&amp;gt;'&lt;/STRONG&gt; and see if that works.&lt;/P&gt;&lt;P&gt;Also, you may want to check out the ArcGIS Python API. It's purpose-built for interacting with GIS portals and servers, and I personally find it easier for querying services than using requests.&lt;/P&gt;</description>
      <pubDate>Thu, 23 Jun 2022 17:03:51 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/how-to-set-up-where-clause-for-date-when/m-p/1185606#M64809</guid>
      <dc:creator>jcarlson</dc:creator>
      <dc:date>2022-06-23T17:03:51Z</dc:date>
    </item>
    <item>
      <title>Re: How to set up where clause for date when extracting data from a map service using python</title>
      <link>https://community.esri.com/t5/python-questions/how-to-set-up-where-clause-for-date-when/m-p/1185633#M64810</link>
      <description>&lt;P&gt;I have a script that takes the current date and subtracts however many days I need off of it, but I'm doing it using a SQL expression.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;"SystemEndDate IS NULL AND Type = 7 AND (GETDATE() - last_edited_date) &amp;lt;= " + str(days) + "&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 23 Jun 2022 17:39:24 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/how-to-set-up-where-clause-for-date-when/m-p/1185633#M64810</guid>
      <dc:creator>Kara_Shindle</dc:creator>
      <dc:date>2022-06-23T17:39:24Z</dc:date>
    </item>
    <item>
      <title>Re: How to set up where clause for date when extracting data from a map service using python</title>
      <link>https://community.esri.com/t5/python-questions/how-to-set-up-where-clause-for-date-when/m-p/1185750#M64812</link>
      <description>&lt;P&gt;You could try something like:&lt;/P&gt;&lt;LI-CODE lang="python"&gt;params = {'where': "CollisionDate &amp;gt;= '{}'".format(datetime.datetime.now() - relativedelta(years=5))}&lt;/LI-CODE&gt;</description>
      <pubDate>Thu, 23 Jun 2022 20:46:41 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/how-to-set-up-where-clause-for-date-when/m-p/1185750#M64812</guid>
      <dc:creator>RViruet</dc:creator>
      <dc:date>2022-06-23T20:46:41Z</dc:date>
    </item>
    <item>
      <title>Re: How to set up where clause for date when extracting data from a map service using python</title>
      <link>https://community.esri.com/t5/python-questions/how-to-set-up-where-clause-for-date-when/m-p/1185805#M64813</link>
      <description>&lt;P&gt;Thank you, Josh, Kara, and RViruet,&lt;/P&gt;&lt;P&gt;I was able to get this to work, using SQL date function:&lt;/P&gt;&lt;P&gt;params = {'where': 'EXTRACT(YEAR from CollisionDate) &amp;gt;= "2017"',&lt;/P&gt;</description>
      <pubDate>Thu, 23 Jun 2022 22:16:03 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/how-to-set-up-where-clause-for-date-when/m-p/1185805#M64813</guid>
      <dc:creator>LoriEmerson_McCormack</dc:creator>
      <dc:date>2022-06-23T22:16:03Z</dc:date>
    </item>
  </channel>
</rss>

