Count months between two timestamp on postgresql?

前端 未结 11 1384
一个人的身影
一个人的身影 2021-02-01 14:50

I want to count the number of months between two dates.

Doing :

SELECT TIMESTAMP \'2012-06-13 10:38:40\' - TIMESTAMP \'2011-04-30 14:38:40\';
         


        
11条回答
  •  忘掉有多难
    2021-02-01 15:32

    age function returns interval:

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

提交回复
热议问题