我有一组与此类似的数据:
我想要做的是创建第三个字段,我们称之为 length
,它采用 created_date
字段并为您提供基于 created_date + months
的日期字段
所以实际上,第三列看起来像这样
但是当我尝试我查找的方式时,它给了我一个错误:
SELECT
DATEADD('month', months, DATE(created_date))
FROM
table
ERROR: function date_add(unknown, double precision, date) does not exist
Hint: No function matches the given name and argument types. You might need to add explicit type casts.
我是否只是使用了错误的语法类型而我应该使用不同的函数?
答案 0 :(得分:1)
注意:根据错误消息,我假设您使用的是 Postgres。
您可以将 add 和 interval
直接转换为 date
值。由于您有固定单位,因此使用 make_interval()
是最简单的方法:
SELECT date_created + make_interval(months => "months"::integer)
FROM the_table
强制转换为 integer
是必要的,因为您无法使用该函数指定小数月份。如果你真的需要,那么以一个月为间隔乘以该值:
SELECT date_created + months * interval '1 month'
FROM the_table
一般而言,我建议将 months
列更改为 interval
,这样您就无需担心此类问题。