微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

postgresql – 在Postgres中添加current_timestamp和days列的总和

我希望通过将天数添加到当前时间来更新列.在pseudoSyntax中它将是:

UPDATE foo
SET time = current_timestamp + days::integer

days是同一个表中的一列.

解决方法

create function add_days_to_timestamp(t timestamptz,d int) 
returns timestamptz
as
$$
begin
    return t + interval '1' day * d;
end; 
$$language 'plpgsql';


create operator + (leftarg = timestamptz,rightarg = int,procedure = add_days_to_timestamp);

在这可行:

update foo set time = current_timestamp + 3 /* day variable here,or a column from your table */

注意:

出于某种原因,在Postgres中内置了一个到目前为止的整数,这可以工作:

select current_timestamp::date + 3 -- but only a date

这不会(除非你定义自己的运算符,见上文):

select current_timestamp + 3

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐