Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
SQLAlchemy: filter by date for an datetime field(does not work with SQLite, with PostgreSQL it works fine)
from datetime import date
from sqlalchemy import cast, DATE
Match.query.filter(cast(Match.date_time_field, DATE)==date.today()).all()
@Fatman13

This comment has been minimized.

Copy link

commented Mar 24, 2017

Same problem here. Might have to do with Sqlite doesn't have a DATE type for its column type.

@jonathanfmachado

This comment has been minimized.

Copy link

commented Mar 12, 2018

Hi guys,
For SQLite, I'm using the func.DATE() function:

from sqlalchemy import func

session.query(db.Transaction).filter(func.DATE(db.Transaction.datetime) == date.today())
@stuartmaxwell

This comment has been minimized.

Copy link

commented Aug 6, 2019

Hi guys,
For SQLite, I'm using the func.DATE() function:

from sqlalchemy import func

session.query(db.Transaction).filter(func.DATE(db.Transaction.datetime) == date.today())

I've been pulling my hair out trying to solve this, but your suggestion worked perfectly. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.