5

I have this model:

class Entry(db.Model):
    title = db.StringProperty()
    url = db.URLProperty()
    date = db.DateTimeProperty(auto_now=True)
    image = db.URLProperty()
    weight = db.IntegerProperty()
    category = db.StringProperty()
    desc = db.TextProperty()

I have lots of entries each day, how do I SELECT only today's entries by using GGL ? since query like this does not return any results ( but I know that there is results ):

SELECT * FROM Entry WHERE category = 'news' and date = '2012-03-12' ORDER BY weight DESC
Linuxios
  • 34,849
  • 13
  • 91
  • 116
Lukas Šalkauskas
  • 14,191
  • 20
  • 61
  • 77

2 Answers2

9

It's not saved as a string, but as a datetime like object.

The right-hand side of a comparison can be one of the following:

  • a datetime, date, or time literal, with either numeric values or a string representation, in the following forms:

    DATETIME(year, month, day, hour, minute, second)
    DATETIME('YYYY-MM-DD HH:MM:SS')
    DATE(year, month, day)
    DATE('YYYY-MM-DD')
    TIME(hour, minute, second)
    TIME('HH:MM:SS')

http://code.google.com/appengine/docs/python/datastore/gqlreference.html

So in your example, use either of these.

date = DATE('2012-03-12')
date = DATE(2012,03,12)

For datetime the time is by default set to 00:00, so equality comparison will fail therefore you must use > to compare

SELECT * FROM Entry WHERE date > DATETIME(yyyy,mm,dd)
bain
  • 1,710
  • 14
  • 15
  • @LukasŠalkauskas: probably because you save as datetime, but query as date, which assumes 00:00:00.000000 for time, and no entry matches that. try >= to test it, or save as date. –  Mar 14 '12 at 11:48
  • 1
    Right, your model says DateTimeProperty so you must use the DATETIME function in GQL. – Guido van Rossum Mar 15 '12 at 03:19
  • Do these work with BigQuery - we are getting an error "DATE function not recognized"? – Praxiteles Jan 09 '18 at 22:25
3

If anyone need to looking for a datetime, it works for me in GQL:

select * from tableName where recordDate >= DATETIME('2018-10-07T00:56:36z')

I hope it help to anyone

Richard Rebeco
  • 753
  • 11
  • 13