convert selected datetime to date in sqlalchemy

That would be by using the cast() expression:

from sqlalchemy import cast, Date

distinct_dates = session.query(cast(Test_Table.test_time, Date)).distinct().all()

or the shortcut method:

distinct_dates = session.query(Test_Table.test_time.cast(Date)).distinct().all()

and if using SQLite, give this a shot:

distinct_dates = session.query(func.DATE(Test_Table.test_time)).distinct().all()

Leave a Comment