【问题标题】:Presto - how is there an alternative to to_char like postgresql?Presto - 如何像 postgresql 一样替代 to_char?
【发布时间】:2020-11-23 23:28:33
【问题描述】:

我需要在 presto 中构建一个可以回顾过去 70 天的查询,我正在使用的表以“YYYYMMDD”的格式存储日期。

在 postgresql 中,我可以简单地将 where 子句写成

where date >= to_char(current_date - 70, 'YYYYMMDD')

它会以 YYYYMMDD 格式提取 70 天前的日期。

但是,在 PrestoSQL 中,该函数似乎不存在,是否有替代方法?

【问题讨论】:

  • 您不应该将 DATE 值存储在 varchar 列中。

标签: sql date presto trino


【解决方案1】:

您可以使用date_format()

where date >= date_format(current_date - interval '70' day, '%Y%m%d')

请注意,将日期存储为字符串根本不是一个好习惯 - 您应该使用正确的 date 类数据类型 - 然后您根本不需要进行转换。

【讨论】:

    【解决方案2】:

    您只需使用日期算术:

    where date >= current_date - interval '70' day
    

    我不确定您为什么要在严格日期相关的比较中包含字符串。

    【讨论】:

    • 我猜 OP 的 date 列的类型是 varchar
    猜你喜欢
    • 2020-07-03
    • 1970-01-01
    • 1970-01-01
    • 2018-12-01
    • 1970-01-01
    • 1970-01-01
    • 2010-09-08
    • 2022-01-09
    • 1970-01-01
    相关资源
    最近更新 更多