使用 Python 脚本从 SQL 数据库返回不正确的日期

Incorrect date being returned from SQL DB with Python script

我有一个 SQL 数据库,我正试图从中提取数据。当我提取 date/time 值时,我的脚本会向 date/time 值添加三个零,如下所示:2011-05-03 15:25:26.170000 以下是我的问题代码:

value_Time = ('SELECT TOP (4) [TimeCol] FROM [database1].[dbo].[table1]')
cursor.execute(value_Time)
for Timerow in cursor:
    print(Timerow)
    Time_list = [elem for elem in Timerow]

期望的结果是 date/time 值的末尾没有额外的三个零,这样我就可以将它插入到不同的数据库中。

Time_List 中的值将包含不正确的 date/time 值以及 Timerow 值。

如有任何帮助,我们将不胜感激!

from datetime import datetime
value_Time = ('SELECT TOP (4) [TimeCol] FROM [database1].[dbo].[table1]')
cursor.execute(value_Time)
row=cursor.fetchone()
for i in range(len(row)):
    var=datetime.strftime(row[i], '%Y-%m-%d %H:%M:%S')
    print(var)

我认为您需要一个包装器来包围您的日期控制示例 "yyyy/mm/dd/hh/mm/ss" 或 "yyyymmddhhmmss"

格式((Datecontrol),"yyyy/mm/dd/hh/mm/ss")