<?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: Iterate Select Layer by Attributes in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/iterate-select-layer-by-attributes/m-p/1097684#M62368</link>
    <description>&lt;P&gt;In python you need to use strftime() from the datetime module to format date fields as a string.&amp;nbsp; &lt;A href="https://thispointer.com/python-how-to-convert-datetime-object-to-string-using-datetime-strftime/" target="_self"&gt;See this for a handy how-to.&lt;/A&gt;&lt;/P&gt;</description>
    <pubDate>Mon, 13 Sep 2021 16:06:52 GMT</pubDate>
    <dc:creator>JoeBorgione</dc:creator>
    <dc:date>2021-09-13T16:06:52Z</dc:date>
    <item>
      <title>Iterate Select Layer by Attributes</title>
      <link>https://community.esri.com/t5/python-questions/iterate-select-layer-by-attributes/m-p/1097681#M62367</link>
      <description>&lt;P&gt;Hi all,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'm a newbie to Python, so I hope the question isn't too dumb.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I want to loop through the date in an attribute field of a point feature class and create new point feature classes for each date. Then, I want to save the layers with names, e.g. 24may98. For instance, I want to have a point layer of 24/5/1998 and so on. I find it tricky to work with as the data type is date/time instead of a string.&lt;/P&gt;&lt;P&gt;I tried to write the code below, but it did work. Thank you in advance for any assistant!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;import arcpy&lt;/P&gt;&lt;P&gt;cursor = arcpy.da.SearchCursor("DBF_ExRainfall","Dates")&lt;/P&gt;&lt;P&gt;for row in cursor:&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; arcpy.management.MakeFeatureLayer(row, Dates_layer)&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; arcpy.management.SelectLayerByAttribute(Dates_layer, "SUBSET_SELECTION","Date = timestamp&amp;nbsp; '{row}'")&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 13 Sep 2021 16:00:23 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/iterate-select-layer-by-attributes/m-p/1097681#M62367</guid>
      <dc:creator>AFung</dc:creator>
      <dc:date>2021-09-13T16:00:23Z</dc:date>
    </item>
    <item>
      <title>Re: Iterate Select Layer by Attributes</title>
      <link>https://community.esri.com/t5/python-questions/iterate-select-layer-by-attributes/m-p/1097684#M62368</link>
      <description>&lt;P&gt;In python you need to use strftime() from the datetime module to format date fields as a string.&amp;nbsp; &lt;A href="https://thispointer.com/python-how-to-convert-datetime-object-to-string-using-datetime-strftime/" target="_self"&gt;See this for a handy how-to.&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 13 Sep 2021 16:06:52 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/iterate-select-layer-by-attributes/m-p/1097684#M62368</guid>
      <dc:creator>JoeBorgione</dc:creator>
      <dc:date>2021-09-13T16:06:52Z</dc:date>
    </item>
    <item>
      <title>Re: Iterate Select Layer by Attributes</title>
      <link>https://community.esri.com/t5/python-questions/iterate-select-layer-by-attributes/m-p/1097947#M62377</link>
      <description>&lt;LI-CODE lang="python"&gt;import arcpy

table = "DBF_ExRainfall"
date_field = "Dates"

# extract the dates from the table and format them:
# list of datetime.datetime objects
# [datetime.datetime(2018, 9, 25, 8, 36, 54), datetime.datetime(2018, 9, 25, 8, 58, 24)]
dates = [row[0] for row in arcpy.da.SearchCursor(table, [date_field], "{} IS NOT NULL".format(date_field))]
# convert to str
# ['2018-09-25', '2018-09-25']
date_strings = [d.strftime("%Y-%m-%d") for d in dates]
# make it distinct
# ['2018-09-25']
distinct_date_strings = list(set(date_strings ))

# create feature layers for each date
for d in distinct_date_strings:
    where_clause = "DATEDIFF(DAY, {}, '{}') = 0".format(date_field, d)
    lyr = arcpy.management.MakeFeatureLayer(table, d, where_clause)
    # if you want it as feature class (saved in database):
    #out_fc = os.path.join(gdb_path, d)
    #arcpy.management.CopyFeatures(lyr, out_fc)&lt;/LI-CODE&gt;</description>
      <pubDate>Tue, 14 Sep 2021 08:59:41 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/iterate-select-layer-by-attributes/m-p/1097947#M62377</guid>
      <dc:creator>JohannesLindner</dc:creator>
      <dc:date>2021-09-14T08:59:41Z</dc:date>
    </item>
    <item>
      <title>Re: Iterate Select Layer by Attributes</title>
      <link>https://community.esri.com/t5/python-questions/iterate-select-layer-by-attributes/m-p/1098089#M62387</link>
      <description>&lt;P&gt;Thanks Johannes!&lt;/P&gt;&lt;P&gt;It solves the problem with the data type! Also, I ran the "Make feature layer" of your script and an error popped up:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Traceback (most recent call last):&lt;BR /&gt;File "C:/Users/aldon/OneDrive/Work/Previsico/Debris Flow Research/GIS Analysis/SBA_Loop_t1.py", line 26, in &amp;lt;module&amp;gt;&lt;BR /&gt;lyr = arcpy.management.MakeFeatureLayer(table, d, where_clause)&lt;BR /&gt;File "C:\Program Files\ArcGIS\Pro\Resources\ArcPy\arcpy\management.py", line 8826, in MakeFeatureLayer&lt;BR /&gt;raise e&lt;BR /&gt;File "C:\Program Files\ArcGIS\Pro\Resources\ArcPy\arcpy\management.py", line 8823, in MakeFeatureLayer&lt;BR /&gt;retval = convertArcObjectToPythonObject(gp.MakeFeatureLayer_management(*gp_fixargs((in_features, out_layer, where_clause, workspace, field_info), True)))&lt;BR /&gt;File "C:\Program Files\ArcGIS\Pro\Resources\ArcPy\arcpy\geoprocessing\_base.py", line 512, in &amp;lt;lambda&amp;gt;&lt;BR /&gt;return lambda *args: val(*gp_fixargs(args, True))&lt;BR /&gt;arcgisscripting.ExecuteError: ERROR 000358: Invalid expression&lt;BR /&gt;Failed to execute (MakeFeatureLayer).&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I spent some time delving into it but I couldn't figure out a valid expression.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Much appreciated!&lt;/P&gt;&lt;P&gt;AFung&lt;/P&gt;</description>
      <pubDate>Tue, 14 Sep 2021 15:22:37 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/iterate-select-layer-by-attributes/m-p/1098089#M62387</guid>
      <dc:creator>AFung</dc:creator>
      <dc:date>2021-09-14T15:22:37Z</dc:date>
    </item>
    <item>
      <title>Re: Iterate Select Layer by Attributes</title>
      <link>https://community.esri.com/t5/python-questions/iterate-select-layer-by-attributes/m-p/1098094#M62388</link>
      <description>&lt;P&gt;Thanks Joe! The strftime() function has worked perfectly fine for solving the data type issue!&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 14 Sep 2021 15:25:06 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/iterate-select-layer-by-attributes/m-p/1098094#M62388</guid>
      <dc:creator>AFung</dc:creator>
      <dc:date>2021-09-14T15:25:06Z</dc:date>
    </item>
    <item>
      <title>Re: Iterate Select Layer by Attributes</title>
      <link>https://community.esri.com/t5/python-questions/iterate-select-layer-by-attributes/m-p/1098337#M62402</link>
      <description>&lt;P&gt;Sorry, I always forget that the sql query depends on the DBMS... The one I used is for SQL Server.&lt;/P&gt;&lt;P&gt;If you're working with a file geodatabase, try this:&lt;/P&gt;&lt;LI-CODE lang="python"&gt;import arcpy
import datetime

table = "DBF_ExRainfall"
date_field = "Dates"

# extract the dates from the table and format them:
# list of datetime.datetime objects
# [datetime.datetime(2018, 9, 25, 8, 36, 54), datetime.datetime(2018, 9, 25, 8, 58, 24)]
dates = [row[0] for row in arcpy.da.SearchCursor(table, [date_field], "{} IS NOT NULL".format(date_field))]
# convert to str
# ['2018-09-25', '2018-09-25']
date_strings = [d.strftime("%Y-%m-%d") for d in dates]
# make it distinct
# ['2018-09-25']
distinct_date_strings = list(set(date_strings ))

# create feature layers for each date
for d in distinct_date_strings:
    start_date = datetime.datetime(*[int(x) for x in d.split("-")])
    end_date = start_date + datetime.timedelta(days=1)
    where_clause = "{0} &amp;gt;= date '{1}' AND {0} &amp;lt; date '{2}'".format(
        date_field,
        start_date.strftime("%Y-%m-%d"),
        end_date.strftime("%Y-%m-%d"))
    lyr = arcpy.management.MakeFeatureLayer(table, d, where_clause)
    # if you want it as feature class (saved in database):
    #out_fc = os.path.join(gdb_path, d)
    #arcpy.management.CopyFeatures(lyr, out_fc)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;If you're working on another DBMS, I can't help you with the query, but here is the SQL reference for ArcGIS which includes syntax for filtering by date: &lt;A href="https://pro.arcgis.com/de/pro-app/latest/help/mapping/navigation/sql-reference-for-elements-used-in-query-expressions.htm" target="_blank"&gt;https://pro.arcgis.com/de/pro-app/latest/help/mapping/navigation/sql-reference-for-elements-used-in-query-expressions.htm&lt;/A&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 15 Sep 2021 07:25:47 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/iterate-select-layer-by-attributes/m-p/1098337#M62402</guid>
      <dc:creator>JohannesLindner</dc:creator>
      <dc:date>2021-09-15T07:25:47Z</dc:date>
    </item>
    <item>
      <title>Re: Iterate Select Layer by Attributes</title>
      <link>https://community.esri.com/t5/python-questions/iterate-select-layer-by-attributes/m-p/1100834#M62530</link>
      <description>&lt;P&gt;Thank you so much for your help! The script works perfectly well!&lt;/P&gt;&lt;P&gt;Sorry that I might have too many questions to ask but would you mind explaining the codes from line 19 to 25 that you added the start date and end date and the where clause query?&lt;/P&gt;&lt;P&gt;Thanks again and have a nice week!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;AFung&lt;/P&gt;</description>
      <pubDate>Wed, 22 Sep 2021 14:35:59 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/iterate-select-layer-by-attributes/m-p/1100834#M62530</guid>
      <dc:creator>AFung</dc:creator>
      <dc:date>2021-09-22T14:35:59Z</dc:date>
    </item>
    <item>
      <title>Re: Iterate Select Layer by Attributes</title>
      <link>https://community.esri.com/t5/python-questions/iterate-select-layer-by-attributes/m-p/1101153#M62533</link>
      <description>&lt;LI-CODE lang="python"&gt;for d in distinct_date_strings:
    # for file geodatabases (SQL 92), the query has to look like this:
    # WHERE DateField &amp;gt;= date '2018-09-25' AND DateField &amp;lt; date '2018-09-26'
    # so "later than the start of this day, but before the start of the next day"

    # I have "this day" in distinct_day_strings, I have to get "next day" from that
    # easiest way to do that is using the datetime.datetime methods
    # to do that, I have to convert "this day" from string back to datetime.datetime

    # in retrospect, I would have done
    # start_date = datetime.datetime.strptime(d, "%Y-%m-%d")
    # but I didn't remember that method, so I did it in a more complicated way...
    
    # d = "2018-09-25"
    # d.split("-") = ["2018", "09", "25"]
    # [int(x) for x in d.split("-")] = [2018, 9, 25]
    # datetime.datetime takes arguments for year, month, day, hour, minute second
    # *[...] unpacks the list into these arguments, it's a shortcut for saing
    # parts = [int(x)...]
    # datetime.datetime(parts[0], parts[1], parts[2], ...)
    start_date = datetime.datetime(*[int(x) for x in d.split("-")])

    # add 1 day to start_date
    end_date = start_date + datetime.timedelta(days=1)

    # construct the where clause from above
    # {0}, {1}, and {2} are placeholders that are filled with the format method
    # the numbers are the indices of the method's arguments
    where_clause = "{0} &amp;gt;= date '{1}' AND {0} &amp;lt; date '{2}'".format(
        date_field,
        start_date.strftime("%Y-%m-%d"),
        end_date.strftime("%Y-%m-%d"))&lt;/LI-CODE&gt;</description>
      <pubDate>Thu, 23 Sep 2021 09:25:24 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/iterate-select-layer-by-attributes/m-p/1101153#M62533</guid>
      <dc:creator>JohannesLindner</dc:creator>
      <dc:date>2021-09-23T09:25:24Z</dc:date>
    </item>
  </channel>
</rss>

