fafz
fafz

Reputation: 81

Python - Date coming from SQL as a list of values

I'm trying to get a date from a table in SQL server but it returns as a list. How can I convert this back into datetime value?

Here's how I'm getting the date:

max_date = engine.connect().execute('select max(receive_datetime) from my_db.dbo.file_table').fetchall()

Here is what that outputs:

>>>[(datetime.datetime(2020, 9, 30, 16, 18, 23),)]

How can I go about converting this to a date with ease? And more importantly am I getting it the wrong way that it comes out as a list? I tried converting this with datetime.strptime but that expects a string and not a list.

Upvotes: 0

Views: 282

Answers (1)

fafz
fafz

Reputation: 81

Resolved

I needed to replace .fetchall() with .scalar()

max_date = engine.connect().execute('select max(receive_datetime) from my_db.dbo.file_table').scalar()

and now the output is datetime instead of a list:

>>> datetime.datetime(2020, 9, 30, 16, 18, 23)

Upvotes: 1

Related Questions