Hernan Acosta
Hernan Acosta

Reputation: 397

SQLAlchemy bounds in between dates

I have the following query :

query_news = sa.select([relaciones_laborales.c.PersonalPerId])
               .where(relaciones_laborales.c.RelLabFch.between(sa.bindparam('p1d'),sa.bindparam('p2d')))

If I'm correct, the bounds in a BETWEEN are not included e.g. range notation (p1d, p2d), and not [p1d, p2d].

Let's say I want to write a condition that represents the range [p1d, p2d) with the RelLabFch attribute greater or equal to p1d and less than p2d, I would need to explicitly write the condition as such:

query_news = sa.select([relaciones_laborales.c.PersonalPerId])
               .where(relaciones_laborales.c.RelLabFch.between(sa.bindparam('p1d'), sa.bindparam('p2d')) | relaciones_laborales.c.RelLabFch == sa.bindparam('p1d'))

Is there a simpler way of writing the above piece of code that represents [p1d, p2d), which is the same as >= p1d AND < p2d?

Upvotes: 0

Views: 2206

Answers (1)

Onel Harrison
Onel Harrison

Reputation: 1334

Using a SQL BETWEEN operator will evaluate ranges in an inclusive manner.

BETWEEN 10 AND 20 is the same as [10, 20].

To write a simple query that represents [10, 20), you can do the following as suggested by Ilja Everilä.

query_news = sa.select([relaciones_laborales.c.PersonalPerId])
               .where(relaciones_laborales.c.RelLabFch >= sa.bindparam('p1d') & \
                      relaciones_laborales.c.RelLabFch < sa.bindparam('p2d'))

Upvotes: 2

Related Questions