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

谁都会走 提交于 2020-01-17 02:51:09

问题


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:


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:

  • Query using two column values to create range


来源:https://stackoverflow.com/questions/38154217/how-do-i-add-a-column-to-a-date-in-postgres

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!