0

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!

8
  • What is the datatype of TimeCol column? Commented Oct 29, 2019 at 12:14
  • datetime @SalmanA Commented Oct 29, 2019 at 12:15
  • 1
    A friendly advice: It's much easier to read sql queries into pandas dataframe with pd.read_sql(), and manipulate the data in pandas Commented Oct 29, 2019 at 12:23
  • 1
    By "an SQL DB" do you mean a Microsoft SQL Server database? Also, what is the "different database" into which you will be inserting the values? Please edit / tag your question as appropriate. Commented Oct 29, 2019 at 13:56
  • Also, please show and not tell us: Values within Time_List will contain the incorrect date/time values, as well as the Timerow value. Commented Oct 29, 2019 at 14:49

2 Answers 2

1
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)
Sign up to request clarification or add additional context in comments.

2 Comments

This results in the following error: TypeError: strptime() argument 1 must be str, not datetime.datetime on the 7th line of code in your example @Vijay
Now it's should work.It was strftime i wrongly wrote strptime.I rewrote my code.
0

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")

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.