Calling function on column value before evaluating filter in Flask and SQLAlchemy

In SQL, your query would be something like this:

SELECT * FROM mytable WHERE date(datetime_column) = '2022-02-03';

To do this in SQLAlchemy, use the same approach: apply the RDBMS’s date function* to the column being filtered.

import sqlalchemy as sa

Table.query(Table).filter( == date_to_be_matched)

* The name of the function that extracts date parts from datetimes may vary by RDBMS, and not all RDBMSs may provide such a function. SQLAlchemy’s func attributes are function names from the RDBMS, so if your RDBMS’s equivalent function is get_date then you would do sa.func.get_date(...).

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top