<?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 Selection by attributes using DatePart() in python in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/selection-by-attributes-using-datepart-in-python/m-p/462080#M36173</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I have a created_date field in my (SDE) feature class. This selection by attributes statement works in ArcMap but I get an "Invalid Expression" error when trying to run it in arcpy. I have printed out the query and it looks to be probably formatted regarding quotations, but obviously I'm not typing it right and I wonder if I need to alter the "DatePart()" part of the formula.&lt;/P&gt;&lt;PRE class="language-python line-numbers"&gt;&lt;CODE&gt;countNumQuery &lt;SPAN class="operator token"&gt;=&lt;/SPAN&gt; &lt;SPAN class="string token"&gt;"DatePart(''"&lt;/SPAN&gt;&lt;SPAN class="string token"&gt;'yyyy'&lt;/SPAN&gt;&lt;SPAN class="string token"&gt;"'', created_date) = {0} AND MonitoringPointID = '{1}'"&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;.&lt;/SPAN&gt;format&lt;SPAN class="punctuation token"&gt;(&lt;/SPAN&gt;countYear&lt;SPAN class="punctuation token"&gt;,&lt;/SPAN&gt; MPID&lt;SPAN class="punctuation token"&gt;)&lt;/SPAN&gt;‍‍‍&lt;SPAN class="line-numbers-rows"&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Prints like this:&lt;/P&gt;&lt;P&gt;DatePart("yyyy", created_date) = 2020 AND MonitoringPointID = '{4FE9BDAB-9EBB-4947-93E5-244170084B54}'&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I've also tried this with no luck&lt;/P&gt;&lt;PRE class="language-python line-numbers"&gt;&lt;CODE&gt;&lt;SPAN class="string token"&gt;"EXTRACT(YEAR FROM created_date) = {0} AND MonitoringPointID = '{1}'"&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;.&lt;/SPAN&gt;format&lt;SPAN class="punctuation token"&gt;(&lt;/SPAN&gt;countYear&lt;SPAN class="punctuation token"&gt;,&lt;/SPAN&gt; MPID&lt;SPAN class="punctuation token"&gt;)&lt;/SPAN&gt;‍‍&lt;SPAN class="line-numbers-rows"&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;countYear is a integer. MonitoringPointID is a GUID. This works in ArcMap:&lt;/P&gt;&lt;P&gt;&lt;IMG alt="" class="jive-emoji image-1 jive-image j-img-original" src="https://community.esri.com/legacyfs/online/489711_Capture.JPG" /&gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Thu, 23 Apr 2020 15:40:45 GMT</pubDate>
    <dc:creator>MKF62</dc:creator>
    <dc:date>2020-04-23T15:40:45Z</dc:date>
    <item>
      <title>Selection by attributes using DatePart() in python</title>
      <link>https://community.esri.com/t5/python-questions/selection-by-attributes-using-datepart-in-python/m-p/462080#M36173</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I have a created_date field in my (SDE) feature class. This selection by attributes statement works in ArcMap but I get an "Invalid Expression" error when trying to run it in arcpy. I have printed out the query and it looks to be probably formatted regarding quotations, but obviously I'm not typing it right and I wonder if I need to alter the "DatePart()" part of the formula.&lt;/P&gt;&lt;PRE class="language-python line-numbers"&gt;&lt;CODE&gt;countNumQuery &lt;SPAN class="operator token"&gt;=&lt;/SPAN&gt; &lt;SPAN class="string token"&gt;"DatePart(''"&lt;/SPAN&gt;&lt;SPAN class="string token"&gt;'yyyy'&lt;/SPAN&gt;&lt;SPAN class="string token"&gt;"'', created_date) = {0} AND MonitoringPointID = '{1}'"&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;.&lt;/SPAN&gt;format&lt;SPAN class="punctuation token"&gt;(&lt;/SPAN&gt;countYear&lt;SPAN class="punctuation token"&gt;,&lt;/SPAN&gt; MPID&lt;SPAN class="punctuation token"&gt;)&lt;/SPAN&gt;‍‍‍&lt;SPAN class="line-numbers-rows"&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Prints like this:&lt;/P&gt;&lt;P&gt;DatePart("yyyy", created_date) = 2020 AND MonitoringPointID = '{4FE9BDAB-9EBB-4947-93E5-244170084B54}'&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I've also tried this with no luck&lt;/P&gt;&lt;PRE class="language-python line-numbers"&gt;&lt;CODE&gt;&lt;SPAN class="string token"&gt;"EXTRACT(YEAR FROM created_date) = {0} AND MonitoringPointID = '{1}'"&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;.&lt;/SPAN&gt;format&lt;SPAN class="punctuation token"&gt;(&lt;/SPAN&gt;countYear&lt;SPAN class="punctuation token"&gt;,&lt;/SPAN&gt; MPID&lt;SPAN class="punctuation token"&gt;)&lt;/SPAN&gt;‍‍&lt;SPAN class="line-numbers-rows"&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;countYear is a integer. MonitoringPointID is a GUID. This works in ArcMap:&lt;/P&gt;&lt;P&gt;&lt;IMG alt="" class="jive-emoji image-1 jive-image j-img-original" src="https://community.esri.com/legacyfs/online/489711_Capture.JPG" /&gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 23 Apr 2020 15:40:45 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/selection-by-attributes-using-datepart-in-python/m-p/462080#M36173</guid>
      <dc:creator>MKF62</dc:creator>
      <dc:date>2020-04-23T15:40:45Z</dc:date>
    </item>
    <item>
      <title>Re: Selection by attributes using DatePart() in python</title>
      <link>https://community.esri.com/t5/python-questions/selection-by-attributes-using-datepart-in-python/m-p/462081#M36174</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Answer:&lt;/P&gt;&lt;PRE class="language-python line-numbers"&gt;&lt;CODE&gt;countNumQuery &lt;SPAN class="operator token"&gt;=&lt;/SPAN&gt; &lt;SPAN class="string token"&gt;"DatePart(year, created_date) = {0} AND MonitoringPointID = '{1}'"&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;.&lt;/SPAN&gt;format&lt;SPAN class="punctuation token"&gt;(&lt;/SPAN&gt;countYear&lt;SPAN class="punctuation token"&gt;,&lt;/SPAN&gt; MPID&lt;SPAN class="punctuation token"&gt;)&lt;/SPAN&gt;‍&lt;SPAN class="line-numbers-rows"&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;The only documentation on DatePart() I could find from ESRI was this page about using it in Field Calculator;&amp;nbsp;&lt;A class="link-titled" href="https://support.esri.com/en/technical-article/000014933" title="https://support.esri.com/en/technical-article/000014933"&gt;How To: Extract a portion of the Date field&lt;/A&gt;, thus the&amp;nbsp;documentation is incomplete. I assume it wasn't working because it was an extraction format of VB, but there's no documentation that I could find that gave examples of python. I found my answer on another GeoNet thread that was tangentially related to my question.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 23 Apr 2020 15:55:45 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/selection-by-attributes-using-datepart-in-python/m-p/462081#M36174</guid>
      <dc:creator>MKF62</dc:creator>
      <dc:date>2020-04-23T15:55:45Z</dc:date>
    </item>
  </channel>
</rss>

