如何在 Postgres 中为日期添加一列?
How do I add a column to a date in Postgres?
我正在使用 Postgres 9。我正在尝试对 table 中的一个整数列进行日期数学计算。我正在尝试这个:
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...
正确的写法是什么?我正在尝试将以年为单位的 age
列添加到当前时间戳(现在)?
将您的 integer
乘以 1 年的时间间隔并将其添加到时间戳:
SELECT current_timestamp + interval '1 year' * age
FROM my_table
WHERE age IS NOT NULL
LIMIT 5;
相关:
- Query using two column values to create range
我正在使用 Postgres 9。我正在尝试对 table 中的一个整数列进行日期数学计算。我正在尝试这个:
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...
正确的写法是什么?我正在尝试将以年为单位的 age
列添加到当前时间戳(现在)?
将您的 integer
乘以 1 年的时间间隔并将其添加到时间戳:
SELECT current_timestamp + interval '1 year' * age
FROM my_table
WHERE age IS NOT NULL
LIMIT 5;
相关:
- Query using two column values to create range