简体   繁体   English

如何在Postgres中向日期添加列?

[英]How do I add a column to a date in Postgres?

I'm using Postgres 9. I'm trying to do date math with a column in my table that is an integer. 我正在使用Postgres9。我试图用表中的整数列进行日期数学运算。 I'm trying this: 我正在尝试:

select current_timestamp + interval age || ' years'
from my_table
where age is not null
limit 5;
 ERROR: syntax error at or near "||" LINE 1: select current_timestamp + interval age || ' years' from rac... 

What is the proper way to write this? 写这个的正确方法是什么? I'm trying to add the age column, which is in years, to the current timestamp (now)? 我正在尝试将age (以年为单位)列添加到当前时间戳记(现在)中?

Multiply your integer with 1-year intervals and add it to the timestamp: 将您的integer乘以1年的时间间隔,并将其添加到时间戳中:

SELECT current_timestamp + interval '1 year' * age
FROM   my_table
WHERE  age IS NOT NULL
LIMIT  5;

Related: 有关:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM