grand2019
grand2019

Reputation: 622

Incorrect date being returned from SQL DB with Python script

I have an SQL DB which I am trying to extract data from. When I extract date/time values my script adds three zeros to the date/time value, like so: 2011-05-03 15:25:26.170000 Below is my code in question:

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]

The desired result is that there is not an additional three zeros at then end of the date/time value so that I can insert it into a different database.

Values within Time_List will contain the incorrect date/time values, as well as the Timerow value.

Any help with this would be much appreciated!

Upvotes: 0

Views: 392

Answers (2)

nector
nector

Reputation: 43

I think you need a wrapper to surround your date control example "yyyy/mm/dd/hh/mm/ss" or "yyyymmddhhmmss"

Format((Datecontrol),"yyyy/mm/dd/hh/mm/ss")

Upvotes: 0

Vijay
Vijay

Reputation: 101

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)

Upvotes: 1

Related Questions