CASE语句中的Amazon Redshift dateadd()

时间:2019-08-13 07:18:35

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

我正在尝试在案例声明中添加日期

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]:Amazon无效的操作:   函数pg_catalog.date_add(“未知”,文本,带时间的时间戳   区域)不存在;

有人可以帮忙吗?

1 个答案:

答案 0 :(得分: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一起使用。这样,您可以为timestamptz保留the_date数据类型:

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