如何在Postgres中向日期添加列? [英] How do I add a column to a date in Postgres?

查看:279
本文介绍了如何在Postgres中向日期添加列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Postgres9.我正在尝试使用表中的整数列进行日期数学运算.我正在尝试:

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...

写这个的正确方法是什么?我正在尝试将age列(以年为单位)添加到当前时间戳(现在)中?

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)?

推荐答案

integer乘以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;

相关:

这篇关于如何在Postgres中向日期添加列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆