<?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: Date return issue with ArcSDESQLExecute in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/date-return-issue-with-arcsdesqlexecute/m-p/1014670#M59435</link>
    <description>&lt;P&gt;Try &lt;A href="https://www.mssqltips.com/sqlservertip/2655/format-sql-server-dates-with-format-function/" target="_self"&gt;formatting the date in the SQL&lt;/A&gt; when you query it. Here's your query string but formatted with Python multiline string.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;sqlString = """SELECT FORMAT([Timestamp], 'yyy-MM-dd HH:mm:ss.fff') as Timestamp,
  [RecordedValue], [GradeName], [ApprovalName]
  FROM {}
  WHERE (SensorID = '{}')
  AND (
    Timestamp BETWEEN CONVERT(datetime, '{}') AND CONVERT(datetime, '{}')
  )
  ORDER BY Timestamp
""" .format(DB_TIMESERIES, senID, start, end)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Sorry, I work with Oracle databases so I apologize if something in that syntax is slightly off.&lt;/P&gt;</description>
    <pubDate>Thu, 07 Jan 2021 16:16:07 GMT</pubDate>
    <dc:creator>BlakeTerhune</dc:creator>
    <dc:date>2021-01-07T16:16:07Z</dc:date>
    <item>
      <title>Date return issue with ArcSDESQLExecute</title>
      <link>https://community.esri.com/t5/python-questions/date-return-issue-with-arcsdesqlexecute/m-p/1014648#M59433</link>
      <description>&lt;P&gt;I am converting a script that uses pyodbc for a couple of select statements to using ArcSDESQLExecute instead. The change works fine on most of the data retrieval but I am hitting an issue on a date field in one of the queries.&lt;/P&gt;&lt;P&gt;With pyodbc, the field is returned as a date object. With ArcSDESQLExecute, the date comes back as a string like '&lt;SPAN&gt;mm/dd/yyyy'&lt;/SPAN&gt;. No hours, minutes, etc. I need either a date object (preferred) or a string with the full %Y%m%d%H%M%S format.&lt;/P&gt;&lt;P&gt;Is there a flag I am missing? A change needed in the SQL statement?&lt;/P&gt;&lt;P&gt;* MS SQL data base&lt;BR /&gt;* Field is type datetime in the database&lt;BR /&gt;* Records are timestamps every ~15 minutes, so having the full date object is important&lt;BR /&gt;* Same SQL string used in both pyodbc and ArcSDESQLExecute and it worked fine before the update.&lt;BR /&gt;&lt;BR /&gt;def _get_timeseries_data(start, end, senID):&lt;/P&gt;&lt;P class="lia-indent-padding-left-30px"&gt;# start = datetime object&lt;BR /&gt;# end = datetime object&lt;BR /&gt;&lt;BR /&gt;DB_TIMESERIES = '[{}].[{}].[timeseries]'.format(DB,SCHEMA)&lt;BR /&gt;DB_CONN = r'C:\Users\blah\blah\blah\Connection to 10.20.2.5.sde'&lt;BR /&gt;cnx = arcpy.ArcSDESQLExecute(DB_CONN)&lt;/P&gt;&lt;P class="lia-indent-padding-left-30px"&gt;# [Timestamp] = table field of type datetime (don't like the naming, but that is from the app vendor)&lt;BR /&gt;# Example from db: 2014-11-17 14:49:01.000&lt;/P&gt;&lt;P class="lia-indent-padding-left-30px"&gt;&lt;BR /&gt;sqlString = "SELECT [Timestamp], [RecordedValue], [GradeName], [ApprovalName] \&lt;BR /&gt;FROM {} WHERE (SensorID = '{}') AND (Timestamp BETWEEN CONVERT(datetime, '{}') AND CONVERT(datetime, '{}'))\&lt;BR /&gt;ORDER BY Timestamp"\&lt;BR /&gt;.format(DB_TIMESERIES, senID, start, end)&lt;BR /&gt;&lt;BR /&gt;rows = cnx.execute(sqlString)&lt;BR /&gt;logging.debug('TS query row: %s', row)&lt;BR /&gt;dRows = []&lt;BR /&gt;for row in rows:&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; dRows.append([row[0], row[1], row[2], row[3]])&lt;/P&gt;&lt;P class="lia-indent-padding-left-30px"&gt;del cnx&lt;BR /&gt;return dRows&lt;/P&gt;</description>
      <pubDate>Thu, 07 Jan 2021 15:35:15 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/date-return-issue-with-arcsdesqlexecute/m-p/1014648#M59433</guid>
      <dc:creator>CassKalinski</dc:creator>
      <dc:date>2021-01-07T15:35:15Z</dc:date>
    </item>
    <item>
      <title>Re: Date return issue with ArcSDESQLExecute</title>
      <link>https://community.esri.com/t5/python-questions/date-return-issue-with-arcsdesqlexecute/m-p/1014653#M59434</link>
      <description>&lt;P&gt;&lt;A href="https://community.esri.com/t5/python-blog/code-formatting-the-community-version/ba-p/1007633" target="_blank"&gt;Code formatting ... the Community Version - GeoNet, The Esri Community&lt;/A&gt;&amp;nbsp;would help readability&lt;/P&gt;</description>
      <pubDate>Thu, 07 Jan 2021 15:40:23 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/date-return-issue-with-arcsdesqlexecute/m-p/1014653#M59434</guid>
      <dc:creator>DanPatterson</dc:creator>
      <dc:date>2021-01-07T15:40:23Z</dc:date>
    </item>
    <item>
      <title>Re: Date return issue with ArcSDESQLExecute</title>
      <link>https://community.esri.com/t5/python-questions/date-return-issue-with-arcsdesqlexecute/m-p/1014670#M59435</link>
      <description>&lt;P&gt;Try &lt;A href="https://www.mssqltips.com/sqlservertip/2655/format-sql-server-dates-with-format-function/" target="_self"&gt;formatting the date in the SQL&lt;/A&gt; when you query it. Here's your query string but formatted with Python multiline string.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;sqlString = """SELECT FORMAT([Timestamp], 'yyy-MM-dd HH:mm:ss.fff') as Timestamp,
  [RecordedValue], [GradeName], [ApprovalName]
  FROM {}
  WHERE (SensorID = '{}')
  AND (
    Timestamp BETWEEN CONVERT(datetime, '{}') AND CONVERT(datetime, '{}')
  )
  ORDER BY Timestamp
""" .format(DB_TIMESERIES, senID, start, end)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Sorry, I work with Oracle databases so I apologize if something in that syntax is slightly off.&lt;/P&gt;</description>
      <pubDate>Thu, 07 Jan 2021 16:16:07 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/date-return-issue-with-arcsdesqlexecute/m-p/1014670#M59435</guid>
      <dc:creator>BlakeTerhune</dc:creator>
      <dc:date>2021-01-07T16:16:07Z</dc:date>
    </item>
    <item>
      <title>Re: Date return issue with ArcSDESQLExecute</title>
      <link>https://community.esri.com/t5/python-questions/date-return-issue-with-arcsdesqlexecute/m-p/1014671#M59436</link>
      <description>&lt;P&gt;The actual code snippet then...&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;def _get_timeseries_data(start, end, senID):

    dRows = []
    cnx = arcpy.ArcSDESQLExecute(DB_CONN)

    sqlString = "SELECT [Timestamp], [RecordedValue], [GradeName], [ApprovalName] \
        FROM {} WHERE (SensorID = '{}') AND (Timestamp BETWEEN CONVERT(datetime, '{}') AND CONVERT(datetime, '{}'))\
            ORDER BY Timestamp"\
            .format(DB_TIMESERIES, senID, start, end)
    try:
        rows = cnx.execute(sqlString)
        for row in rows:
            logging.debug('TS query row: %s', row)
            # With pyodbc, used fTimestamp instead of row[0] in the append...
            # fTimestamp = row.Timestamp.strftime("%Y-%m-%d %H:%M")
            fValue = _format_readings(row[1])
            dRows.append([row[0],
                fValue,
                row[2],
                row[3]])

    except Exception as e:        
        print('SQL call failed. SQL: %s. Error: %s', sqlString, e)

    del cnx

    return dRows&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 07 Jan 2021 15:58:46 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/date-return-issue-with-arcsdesqlexecute/m-p/1014671#M59436</guid>
      <dc:creator>CassKalinski</dc:creator>
      <dc:date>2021-01-07T15:58:46Z</dc:date>
    </item>
    <item>
      <title>Re: Date return issue with ArcSDESQLExecute</title>
      <link>https://community.esri.com/t5/python-questions/date-return-issue-with-arcsdesqlexecute/m-p/1014687#M59437</link>
      <description>&lt;P&gt;Excellent! Worked as advertised.&lt;BR /&gt;Thank you Blake!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 07 Jan 2021 16:21:00 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/date-return-issue-with-arcsdesqlexecute/m-p/1014687#M59437</guid>
      <dc:creator>CassKalinski</dc:creator>
      <dc:date>2021-01-07T16:21:00Z</dc:date>
    </item>
  </channel>
</rss>

