postgresql 通过添加月份来计算 Postgres 中的日期?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5909363/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Calculating a date in Postgres by adding months?
提问by Elitmiar
I have a postgres table that has the following fields
我有一个具有以下字段的 postgres 表
start_date,duration
开始日期,持续时间
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.
持续时间包含任意数量的月份,因此要计算结束日期,请将持续时间中的月份添加到开始日期。现在我想用 postgres 查询做这样的事情。
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?
这是可能的,如何正确使用语法?
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)
我的postgres的版本是 PostgreSQL 8.1.22 on x86_64-redhat-linux-gnu,由GCC gcc (GCC) 4.1.2 20080704 (Red Hat 4.1.2-48)编译
回答by manji
try:
尝试:
(start_date + (duration * '1 month'::INTERVAL)) < '2010-05-12'
or
或者
(start_date + (duration || ' month')::INTERVAL) < '2010-05-12'
More info: Date/Time Functions and Operators
更多信息:日期/时间函数和运算符
回答by Arj
You might find this article useful: PostgreSQL: month := interval '30 days';
您可能会发现这篇文章很有用:PostgreSQL: month := interval '30 days';
Also this link: http://wiki.postgresql.org/wiki/Working_with_Dates_and_Times_in_PostgreSQL, more specifically the section called "WORKING with DATETIME, DATE, and INTERVAL VALUES".
还有这个链接:http: //wiki.postgresql.org/wiki/Working_with_Dates_and_Times_in_PostgreSQL,更具体地说是名为“WORKING with DATETIME, DATE, and INTERVAL VALUES”的部分。