Selecting records between two timestamps

前端 未结 2 392
囚心锁ツ
囚心锁ツ 2021-01-01 12:25

I am converting an Unix script with a SQL transact command to a PostgreSQL command.

I have a table with records that have a field last_update_time(xtime

相关标签:
2条回答
  • 2021-01-01 12:43

    For anyone who is looking for the fix to this. You need to remove timestamp from the where clause and use BETWEEN!

    TABLENAME.COL-NAME-FOR-TIMESTAMP BETWEEN '2020-01-29 04:18:00-06' AND CURRENT_TIMESTAMP
    
    0 讨论(0)
  • 2021-01-01 12:48

    What's wrong with:

    SELECT a,b,c
    FROM   table
    WHERE  xtime BETWEEN '2012-04-01 23:55:00'::timestamp
                     AND now()::timestamp;
    

    If you want to operate with a count of seconds as interval:

    ...
    WHERE  xtime BETWEEN now()::timestamp - (interval '1s') * $selectedtimeParm
                     AND now()::timestamp;
    

    Note, how I used the standard ISO 8601 date format YYYY-MM-DD h24:mi:ss which is unambiguous with any locale or DateStyle setting.

    Note also, that the first value for the BETWEEN construct must be the smaller one. If you don't know which value is smaller use BETWEEN SYMMETRIC instead.

    In your question you refer to the datetime type timestamp as "date", "time" and "period". In the title you used the term "time frames", which I changed that to "timestamps". All of these terms are wrong. Freely interchanging them makes the question even harder to understand.

    That, and the fact that you only tagged the question psql (the problem hardly concerns the command line terminal) might help to explain why nobody answered for days. Normally, it's a matter of minutes around here. I had a hard time understanding your question, had to read it a couple of times.

    You need to understand the data types date, interval, time and timestamp - with or without time zone. Start by reading the chapter "Date/Time Types" in the manual.

    Error message would have gone a long way, too.

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