-1

I have below python code as sql query to update a record in table:

query = "UPDATE mytable SET data='{}' where id='{}'".format(value, id)

This updated the record in table. Now I have another query which include datetime:

send_dt = datetime.utcnow()
query = "UPDATE mytable SET data='{}', send_datetime='{}' where id='{}'".format(value, send_dt, id)

Now because I have include send_dt inside format, it is making the type datetime to string and thus I am getting below error:

Conversion failed when converting date and/or time from character string.

How can I update my query so that datetime remains datetime and not string. Thanks

S Andrew
  • 5,592
  • 27
  • 115
  • 237

1 Answers1

-1

use to_date function to convert into date format. Somthing like this.

send_dt = datetime.utcnow()
query = "UPDATE mytable SET data='{}', send_datetime=to_date('{}','YYYY-MM-DD hh24:mi:ss.FF9') where id='{}'".format(value, str(send_dt), id)

or instead of calculating the date use the predefined function in the sql.

query = "UPDATE mytable SET data='{}', send_datetime=sysdate where id='{}'".format(value, id)