How I can filter out Saturday and Sunday?

494 views
Skip to first unread message

Gabriel Pozo

unread,
Dec 17, 2012, 2:54:20 PM12/17/12
to sqlal...@googlegroups.com
Hello everyone,

I am a new user of sqlalchemy, I need to know how I can filter the weekdays, to exclude Saturdays and Sundays.
I look for something similar to the function dayofweek of MySQL.

Thank you all, and sorry for my bad English

Regards !!!

Audrius Kažukauskas

unread,
Dec 17, 2012, 3:08:39 PM12/17/12
to sqlal...@googlegroups.com
On Mon, 2012-12-17 at 11:54:20 -0800, Gabriel Pozo wrote:
> I am a new user of sqlalchemy, I need to know how I can filter the
> weekdays, to exclude Saturdays and Sundays.
> I look for something similar to the function dayofweek of MySQL.

In SQLAchemy you can refer to DB functions via func[0] construct. Say,
you have the following model:

class Model(Base):
__tablename__ = 'foo'
id = Column(Integer, primary_key=True)
date = Column(DateTime)

You can query it like this:

session.query(Model).filter(
# 1 = Sunday, 2 = Monday, ..., 7 = Saturday.
func.dayofweek(Model.date).between(2, 6)
).all()

[0] http://docs.sqlalchemy.org/en/rel_0_8/core/expression_api.html#sqlalchemy.sql.expression.func

--
Audrius Kažukauskas
http://neutrino.lt/

Gabriel Pozo

unread,
Dec 17, 2012, 3:33:52 PM12/17/12
to sqlal...@googlegroups.com
Thanks Audrius,

Your answer was very helpful, is exactly what I was looking.


2012/12/17 Audrius Kažukauskas <aud...@neutrino.lt>
Reply all
Reply to author
Forward
0 new messages