在PostgreSQL的两个时间戳之间计算月份? [英] Count months between two timestamp on postgresql?
本文介绍了在PostgreSQL的两个时间戳之间计算月份?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想计算两个日期之间的月数。
I want to count the number of months between two dates.
这样做:
SELECT TIMESTAMP '2012-06-13 10:38:40' - TIMESTAMP '2011-04-30 14:38:40';
返回值:
0年0星期一409天20小时0分钟0.00秒
Returns : 0 years 0 mons 409 days 20 hours 0 mins 0.00 secs
等等:
SELECT extract(month from TIMESTAMP '2012-06-13 10:38:40' - TIMESTAMP '2011-04-30 14:38:40');
返回0。
推荐答案
age
函数返回时间间隔:
age(timestamp1, timestamp2)
然后我们尝试从间隔中提取年和月,并相应地添加它们:
Then we try to extract year and month out of the interval and add them accordingly:
select extract(year from age(timestamp1, timestamp2)) * 12 +
extract(month from age(timestamp1, timestamp2))
这篇关于在PostgreSQL的两个时间戳之间计算月份?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文