Unexpected results from SQL query with BETWEEN timestamps

前端 未结 1 1848
太阳男子
太阳男子 2021-01-29 01:34

I have created a little test app to track down a problem I experienced with Postgres on Heroku: http://snippi.com/s/xd511rf

As you can see in line 49, I

相关标签:
1条回答
  • 2021-01-29 02:35

    Check the data type of the columns and your time zone. You may be confusing timestamp with time zone and timestamp.

    Looks like you have timestamp in your table, but query with timestamptz. This way, it all depends on the local time zone of your session (which defaults to the time zone of the server if not specified otherwise.)

    Switch both to timestamptz, or timestamp if time zones are completely irrelevant to you. (If in doubt, use timestamptz.)

    Not the cause of your problem, but your query should probably be:

    SELECT id, text, category, starttime, endtime, creation 
    FROM   entries 
    WHERE  starttime >= timestamp '2013-03-21' -- defaults to 00:00 time
    AND    starttime <  timestamp '2013-03-22'
    ORDER  BY id
    

    a BETWEEN x AND y is almost always wrong for timestamp types due to fractional numbers! What would your query do with starttime = '2013-03-21T23:59:59.123+00'?

    0 讨论(0)
提交回复
热议问题