Count months between two timestamp on postgresql?

前端 未结 11 1167
一个人的身影
一个人的身影 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))
    
    0 讨论(0)
  • 2021-02-01 15:36

    Please note that the most voted answer by @ram and @angelin is not accurate when you are trying to get calendar month difference using.

    select extract(year from age(timestamp1, timestamp2))*12 + extract(month from age(timestamp1, timestamp2))
    

    for example, if you try to do:

    select extract(year from age('2018-02-02'::date, '2018-03-01'::date))*12 + extract(month from age('2018-02-02'::date , '2018-03-01'::date))
    

    the result will be 0 but in terms of months between March from February should be 1 no matter the days between dates.

    so the formula should be like the following saying that we start with timestamp1 and timestamp2:

    ((year2 - year1)*12) - month1 + month2 = calendar months between two timestamps

    in pg that would be translated to:

    select ((extract('years' from '2018-03-01 00:00:00'::timestamp)::int -  extract('years' from '2018-02-02 00:00:00'::timestamp)::int) * 12) 
        - extract('month' from '2018-02-02 00:00:00'::timestamp)::int + extract('month' from '2018-03-01 00:00:00'::timestamp)::int;
    

    you can create a function like:

    CREATE FUNCTION months_between (t_start timestamp, t_end timestamp)
    RETURNS integer
    AS $$
    select ((extract('years' from $2)::int -  extract('years' from $1)::int) * 12) 
        - extract('month' from $1)::int + extract('month' from $2)::int
    $$
    LANGUAGE SQL
    IMMUTABLE
    RETURNS NULL ON NULL INPUT;
    
    0 讨论(0)
  • 2021-02-01 15:36
    SELECT date_part ('year', f) * 12
          + date_part ('month', f)
    FROM age ('2015-06-12', '2014-12-01') f
    

    Result: 6 Months

    0 讨论(0)
  • 2021-02-01 15:36

    Try this solution:

    SELECT extract (MONTH FROM age('2014-03-03 00:00:00'::timestamp, 
    '2013-02-03 00:00:00'::timestamp)) + 12 * extract (YEAR FROM age('2014-03-03   
    00:00:00'::timestamp, '2013-02-03 00:00:00'::timestamp)) as age_in_month;
    
    0 讨论(0)
  • 2021-02-01 15:36
    SELECT floor(extract(days from TIMESTAMP '2012-06-13 10:38:40' - TIMESTAMP
    '2011-04-30 14:38:40')/30.43)::integer as months;
    

    Gives an approximate value but avoids duplication of timestamps. This uses hint from tobixen's answer to divide by 30.43 in place of 30 to be less incorrect for long timespans while computing months.

    0 讨论(0)
提交回复
热议问题