<?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 Concatenate Date Time in SelectByAttribute in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/concatenate-date-time-in-selectbyattribute/m-p/1120427#M63068</link>
    <description>&lt;P&gt;Hi,&amp;nbsp;&lt;/P&gt;&lt;P&gt;I need to concatenate Data in Python 3, the script below is in VB.&lt;/P&gt;&lt;P&gt;The PLANTIO field is with Date type and SAFRA text, but validation needs to be like the expression below. I've already tried transforming in ArcPRO Modelbuilder but without success.&lt;/P&gt;&lt;P&gt;Ano1 and Ano2 = Text&lt;/P&gt;&lt;P&gt;How can I do this?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;arcpy.SelectLayerByAttribute_management("Censo_view1", "NEW_SELECTION", "!SAFRA! = '" + Safra2 + "' AND !PLANTIO! &amp;gt;= #04-01-" + ano + " 00:00:00# AND !PLANTIO! &amp;lt;= #03-31-" + ano1 + " 00:00:00#")&lt;/P&gt;</description>
    <pubDate>Fri, 26 Nov 2021 13:27:54 GMT</pubDate>
    <dc:creator>SoratoSouza_e_Silva</dc:creator>
    <dc:date>2021-11-26T13:27:54Z</dc:date>
    <item>
      <title>Concatenate Date Time in SelectByAttribute</title>
      <link>https://community.esri.com/t5/python-questions/concatenate-date-time-in-selectbyattribute/m-p/1120427#M63068</link>
      <description>&lt;P&gt;Hi,&amp;nbsp;&lt;/P&gt;&lt;P&gt;I need to concatenate Data in Python 3, the script below is in VB.&lt;/P&gt;&lt;P&gt;The PLANTIO field is with Date type and SAFRA text, but validation needs to be like the expression below. I've already tried transforming in ArcPRO Modelbuilder but without success.&lt;/P&gt;&lt;P&gt;Ano1 and Ano2 = Text&lt;/P&gt;&lt;P&gt;How can I do this?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;arcpy.SelectLayerByAttribute_management("Censo_view1", "NEW_SELECTION", "!SAFRA! = '" + Safra2 + "' AND !PLANTIO! &amp;gt;= #04-01-" + ano + " 00:00:00# AND !PLANTIO! &amp;lt;= #03-31-" + ano1 + " 00:00:00#")&lt;/P&gt;</description>
      <pubDate>Fri, 26 Nov 2021 13:27:54 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/concatenate-date-time-in-selectbyattribute/m-p/1120427#M63068</guid>
      <dc:creator>SoratoSouza_e_Silva</dc:creator>
      <dc:date>2021-11-26T13:27:54Z</dc:date>
    </item>
    <item>
      <title>Re: Concatenate Date Time in SelectByAttribute</title>
      <link>https://community.esri.com/t5/python-questions/concatenate-date-time-in-selectbyattribute/m-p/1120780#M63093</link>
      <description>&lt;P&gt;Your best bet is probably creating datetime objects from the strings and letting SQL handle the date comparisons. Check the documentation for formatting sql queries that include dates, can't guarantee this example will work but should give you an idea how to do this.&amp;nbsp; Also check where how you have your &amp;gt;= signs, it looks like it wants everything after 4/1 and everything before 3/31.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;Safra2 = 'value'
anodte = datetime.datetime.strptime(f'04-01-{ano} 00:00:00', '%m-%d-%Y %H:%M:%S')
anodte1 = datetime.datetime.strptime(f'03-31-{ano1} 00:00:00', '%m-%d-%Y %H:%M:%S')

sql = f"SAFRA = '{Safra2}' AND PLANTIO &amp;gt;= {anodte1} AND PLANTIO &amp;lt;= {anodte}"

# or use SQL BETWEEN
sql = f"SAFRA = '{Safra2}' AND PLANTIO BETWEEN {anodte1} AND {anodte}"

arcpy.SelectLayerByAttribute_management("Censo_view1", "NEW_SELECTION", sql)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 29 Nov 2021 14:25:30 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/concatenate-date-time-in-selectbyattribute/m-p/1120780#M63093</guid>
      <dc:creator>Anonymous User</dc:creator>
      <dc:date>2021-11-29T14:25:30Z</dc:date>
    </item>
  </channel>
</rss>

