【问题标题】:postgresl SQL statement displays dates outside the intervalpostgresql SQL 语句显示区间外的数据
【发布时间】:2020-05-07 13:31:43
【问题描述】:

在 RHEL 上,命令提示符处的日期/时间采用 UTC 时间。 我在表中查询最后一小时的日期,但它返回给我其他日期/时间。我不知道它是否与时区有关,但我非常困惑如何使用它

select to_char(update_dt, 'DD Mon YYYY HH12:MI'),data 
from mytable
where update_dt > current_date - interval '1' hour


      to_char      |   data
-------------------+----------------
 07 May 2020 12:37 | blah
 07 May 2020 12:37 | blah
 07 May 2020 12:37 | blah
 07 May 2020 12:37 | blah
 07 May 2020 12:37 | blah
 07 May 2020 05:23 | huh
 07 May 2020 05:23 | huh
 07 May 2020 05:22 | huh

[root@ip-172-31-1-28 ~]# date
Thu May  7 13:25:03 UTC 2020

【问题讨论】:

    标签: sql postgresql utc


    【解决方案1】:

    这个表达式:

    where update_dt > current_date - interval '1' hour
    

    从昨天午夜开始,因为current_date 只是午夜的日期(在日期的开头)。

    您似乎希望包含时间

    where update_dt > now() - interval '1' hour
    

    你也可以使用:

    where update_dt > current_timestamp - interval '1' hour
    

    now() 更容易输入。

    【讨论】:

    • 当然,谢谢!当stackoverflow让我大声笑时我会接受
    猜你喜欢
    • 2012-05-06
    • 1970-01-01
    • 1970-01-01
    • 2020-01-18
    • 2021-03-11
    • 1970-01-01
    • 2014-08-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多