date range in query

36 views
Skip to first unread message

arnie

unread,
Feb 3, 2009, 6:02:45 AM2/3/09
to Google App Engine
For a datastore table with two db.DateTimeProperty columns start_date
and end_date, i need to use a date range in query but below given
query is not working
current_date=datetime.datetime.strptime(self.request.get
('current_date'), '%Y-%m-%dT%H:%M:%S')

db.GqlQuery('SELECT * FROM myTable where start_date>=:cdate and
end_date<=:cdate',cdate=current_date)
in the parameters I am passing current_date=2009-01-10T00:00:00
Should we not compare dates this way?
But this is not working

N. Rosencrantz

unread,
Feb 3, 2009, 6:17:17 AM2/3/09
to Google App Engine
gql as below works.
SELECT * FROM Image where added > DATETIME(2009,1,1,0,0,0) and added <
DATETIME(2009,2,1,0,0,0)
we must use the integer form (the manual lists b0rked forms)
http://code.google.com/p/googleappengine/issues/detail?id=878
cheers
niklas

Arun Shanker Prasad

unread,
Feb 3, 2009, 11:43:29 PM2/3/09
to Google App Engine
Hi,

db.GqlQuery('SELECT * FROM myTable where start_date>=:cdate and
end_date<=:cdate',cdate=current_date)

You are trying to use in-equality filters on two properties in a
single query.. Does this not raise an Exception?

There was a post on how to do this more efficiently in the groups
somewhere, but in your case I think you need to query based on one
date from the datastore and do some logic in the view.

> SELECT * FROM Image where added > DATETIME(2009,1,1,0,0,0) and added <
> DATETIME(2009,2,1,0,0,0)

In this example both are applied to the same property.

Thanks,
Arun Shanker Prasad.

On Feb 3, 4:17 pm, niklasr <nikla...@gmail.com> wrote:
> gql as below works.
> SELECT * FROM Image where added > DATETIME(2009,1,1,0,0,0) and added <
> DATETIME(2009,2,1,0,0,0)
> we must use the integer form (the manual lists b0rked forms)http://code.google.com/p/googleappengine/issues/detail?id=878

arnie

unread,
Feb 4, 2009, 5:47:39 AM2/4/09
to Google App Engine
I have applied the DATETIME method as suggested but unable to use
inequality operator for 2 datetime fields.
I have tried this too
b=db.gqlquery('SELECT * FROM myTable WHERE field1=:val1 and
sdate>=DATETIME(2009,11,20,0,0,0)')
b=b.filter('where edate<=DATETIME(2009,11,20,0,0,0)')
But it does not work. How can I use the comparsion so as to get
results based on using the two table fields?
Thanks

Garrett Davis

unread,
Feb 5, 2009, 9:15:25 AM2/5/09
to Google App Engine
You are right; you can't have a query with inequality operators on two
fields.
But you can get the result you want, if you add another field to your
object that holds both dates.
There's a new entry in the Google App Engine Cookbook that shows how:
http://appengine-cookbook.appspot.com/recipe/how-to-query-by-date-range

Garrett Davis
Reply all
Reply to author
Forward
0 new messages