【问题标题】:Subtracting Day from Postgresql EPOCH Column从 Postgresql EPOCH 列中减去天
【发布时间】:2021-05-08 05:17:58
【问题描述】:

我有以下代码,我试图简单地从日期中减去 5 天。日期在 t.Date_created 字段中存储为 EPOCH 时间(毫秒,13 个数字)。但由于某种原因,代码不适用于以下错误。任何建议都会有所帮助!!!

[42883] 错误:运营商不存在:带时区的时间戳 - 整数提示:没有运算符匹配给定的名称和参数类型。 您可能需要添加显式类型转换。

代码如下:

SELECT to_timestamp(t.date_Created / 1000) - 5 FROM task_mgmt.teams t LIMIT 5;

【问题讨论】:

  • 如果你只对日期感兴趣,那么:SELECT to_timestamp(t.date_Created / 1000)::date - 5 FROM task_mgmt.teams t LIMIT 5;

标签: sql postgresql epoch


【解决方案1】:

你需要减去一个interval

to_timestamp(t.date_Created / 1000) - interval '5 days'

整数只能直接从date 值中减去,而不能从timestamp 值中减去。

【讨论】:

  • @Blackdynomite:为什么不将时间戳存储为正确的timestamptz?然后你也可以摆脱timestamp值和bigint之间的转换
  • 对此相对较新,所以不知道这是一个选项!我将不得不调查如何做到这一点
  • @Blackdynomite: alter table teams alter date_created type timestamp using to_timestamp(date_created/1000); 您可能还想阅读this
【解决方案2】:

我建议算术:

t.date_created - 5 * 24 * 60 * 60 * 1000

【讨论】:

    猜你喜欢
    • 2022-08-04
    • 2018-11-24
    • 2012-06-24
    • 1970-01-01
    • 2018-03-04
    • 1970-01-01
    • 1970-01-01
    • 2021-05-12
    • 2012-10-07
    相关资源
    最近更新 更多