简体   繁体   中英

Filter sqlalchemy sqlite datetime column by date

I have a datetime column in a database (Named TestBase) like so:

INDEX   DateTimeColumn
...     ...
1241    2010-07-26 07:04:05 
1242    2010-07-26 07:04:07 
1243    2010-07-26 07:04:12 
1244    2010-07-27 10:59:53

I want to get all the unique days in the column.

However, using the distinct function in sqlalchemy as such: distinct(TestBase.DateTimeColumn) does not work. As it takes the time within the timestamp into account and considers every entry to be unique.

How do I write a query which ignores the time within a timestamp and just returns the list of unique dates?

Use the database's DATE function to truncate the datetime to a date. The DATE function is accessed through sqlalchemy.sql.func :

from sqlalchemy import sql

q = session.query(sql.func.date(MyModel.datetimecolumn))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM