How do I add a column to a date in Postgres?

2019-09-17 06:44发布

I’m using Postgres 9. I’m trying to do date math with a column in my table that is an integer. 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)?

1条回答
Ridiculous、
2楼-- · 2019-09-17 07:35

Multiply your integer with 1-year intervals and add it to the timestamp:

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

Related:

查看更多
登录 后发表回答