How to get month and year from Date field in sqlalchemy?

You can use following constructs to filter the Date column using either year or month:

.filter(extract('year', Foo.Date) == 2012)
.filter(extract('month', Foo.Date) == 12)

And group_by is also possible:

.group_by(sqlalchemy.func.year(Foo.Date), sqlalchemy.func.month(Foo.Date))

Now I haven’t tested it, but I assume that this might be slow because these queries result in full table scans, therefore I suggest you invest some time and learn how to use composite columns.

Note: extract() is imported from sqlalchemy.sql

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)