【问题标题】:Amazon Redshift dateadd() in CASE statementCASE 语句中的 Amazon Redshift dateadd()
【发布时间】:2019-12-19 17:23:13
【问题描述】:

我正在尝试在案例陈述中添加日期

select the_date, 
(case 
when the_date like '%years%' then dateadd(year,REPLACE(REGEXP_SUBSTR(condition_, '-?[0-9]+\.? years'),'years',''),the_date)
end) 
from customer

但我收到以下错误:

SQL 错误 [500310] [42883]:亚马逊无效操作: function pg_catalog.date_add("unknown", text, timestamp with time zone) 不存在;

有人可以帮忙吗?

【问题讨论】:

    标签: amazon-web-services date amazon-redshift


    【解决方案1】:

    您必须将第二个参数转换为INT,将第三个参数转换为timestamp(当前为timestamptz。

    这是DATEADD签名:

    DATEADD( datepart, interval, {date|timestamp} )
    

    您的查询应该类似于:

    select the_date, 
    (case when the_date like '%years%' then 
        dateadd(
             year,
             REPLACE(REGEXP_SUBSTR(condition_, '-?[0-9]+\.? years'),'years','')::INT,
             the_date::timestamp
        )
    end) 
    from customer
    

    或者,您可以使用简单的+ 运算符和interval。这样您就可以为the_date 保留timestamptz 数据类型:

    select the_date, 
    (case when the_date like '%years%' then 
        the_date + (interval '1 year' * REPLACE(REGEXP_SUBSTR(condition_, '-?[0-9]+\.? years'),'years','')::INT)
        )
    end) 
    from customer
    

    【讨论】:

    • 谢谢你,但不幸的是仍然出现同样的错误,SQL 错误 [500310] [42883]: [Amazon](500310) 无效操作:函数 pg_catalog.date_add("unknown", integer, timestamp with time区域)不存在;
    • 您还必须将 the_date 从 timestamptz 转换为 timestamp
    • 您也可以使用简单的+ 和一个间隔,因为它受timestamptz 数据类型的支持。我将添加一个示例。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多