Skip to content

Instantly share code, notes, and snippets.

@danielthiel
Last active February 12, 2021 13:06
Show Gist options
  • Star 12 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save danielthiel/8374607 to your computer and use it in GitHub Desktop.
Save danielthiel/8374607 to your computer and use it in GitHub Desktop.
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()
@arqeco
Copy link

arqeco commented Jan 18, 2020

Thank you Jonatan. func.DATE() works.
I've opened an issue sqlalchemy/sqlalchemy#5104

@AlejandroRodriguezP
Copy link

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())

It worked! Thank you sooo much!

@3yggy
Copy link

3yggy commented Sep 9, 2020

👺it doesn't work with even postgress for me

@FabricioPatrocinio
Copy link

FabricioPatrocinio commented Feb 12, 2021

This worked for me:

from sqlalchemy import func
table_db = Youtable.query.filter(func.DATE(Youtable.date_bd) == your_date)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment