通过添加月份来计算Postgres中的日期? [英] Calculating a date in Postgres by adding months?

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

问题描述

我有一个包含以下字段的postgres表

I have a postgres table that has the following fields

开始日期,持续时间

持续时间包含任意数量的月数,因此要计算结束日期,请将持续时间的月数添加到开始日期。现在,我想用postgres查询做类似的事情。

duration contains any number of months, so to calculate the end date you add the months in duration to the start date. Now I want to do something like this with a postgres query.

SELECT * 
FROM table 
WHERE start_date > '2010-05-12' 
AND (start_date + duration) < '2010-05-12'

这可能吗?如何正确使用语法?

Is this possible and how does one right the syntax?

我的postgres版本是 x86_64-redhat-linux-gnu上的PostgreSQL 8.1.22,由GCC gcc(GCC)4.1.2 20080704(Red Hat 4.1。 2-48)

The version of my postgres is PostgreSQL 8.1.22 on x86_64-redhat-linux-gnu, compiled by GCC gcc (GCC) 4.1.2 20080704 (Red Hat 4.1.2-48)

推荐答案

尝试:

(start_date + (duration * '1 month'::INTERVAL)) < '2010-05-12'

(start_date + (duration || ' month')::INTERVAL) < '2010-05-12'

更多信息: 日期/时间函数和运算符

More info: Date/Time Functions and Operators

这篇关于通过添加月份来计算Postgres中的日期?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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