Ram Rahamim
Ram Rahamim

Reputation: 49

SELECT timestamp from SQL query in python

cur.execute('Select ts from testtable where nd_a>%s and nd_b>%s and nd_c>%s',(medA,medB,medC))
result_ts=cur.fetchall()
print (result_ts)

when i get the output this is look like this:

((datetime.datetime(2020, 1, 1, 1, 15, 24),), (datetime.datetime(2020, 1, 1, 1, 15, 38),), (datetime.datetime(2020, 1, 1, 1, 16, 30),), (datetime.datetime(2020, 1, 1, 1, 16, 37),), (datetime.datetime(2020, 1, 1, 1, 17, 8),), (datetime.datetime(2020, 1, 1, 1, 17, 14),))

When I need the output date in this format: YYYY-MM-DD HH:MM:SS

How I change it to this format view?

Upvotes: 0

Views: 3028

Answers (2)

Sam
Sam

Reputation: 475

If you'd like a string timestamp, you should use the strftime method.

cur.execute('Select ts from testtable where nd_a>%s and nd_b>%s and nd_c>%s',(medA,medB,medC))
result_ts=cur.fetchall()
timestamps = []
for r in results_ts:
    timestamps.append(r[0].strftime('%Y-%m-%d %H:%M:%s'))

This will give you a list of results translated into timestamp strings :)

Upvotes: 0

Swetank Poddar
Swetank Poddar

Reputation: 1291

You could do something like this:

cur.execute('Select ts from testtable where nd_a>%s and nd_b>%s and nd_c>%s',(medA,medB,medC))
result_ts=cur.fetchall()

for result in result_ts:
    print(result[0])

This will print:

2020-01-01 01:15:24
2020-01-01 01:15:38
2020-01-01 01:16:30
2020-01-01 01:16:37
2020-01-01 01:17:08
2020-01-01 01:17:14

Upvotes: 1

Related Questions