user3527975 user3527975 - 6 months ago 67
SQL Question

Convert func.now() from sqlalchemy to a string

Is it possible to convert the result of func.now() from sqlalchemy to a string object? Something along the lines of

datetime.datetime.now().strftime('%m/%d/%Y')


I want to use this result in a raw sqlalchemy that updates a datetime column in mysql. This query accepts only string parameters.

Answer

Using str(func.now()) returns now() which is the SQL function for using the current timestamp. In other words, if you're constructing a raw query you can pass that in directly and MySQL will automatically fill in the current timestamp. If you want to pass in something along the lines of %m/%d/%Y, then func.now is not the appropriate tool to use.

Comments