PostgreSQL: days/months/years between two dates

后端 未结 10 1982
旧时难觅i
旧时难觅i 2020-12-12 20:22

I am looking for a way to implement the SQLServer-function datediff in PostgreSQL. That is,

This function returns the count (as a signed integer valu

10条回答
  •  失恋的感觉
    2020-12-12 20:32

    Here is a complete example with output. psql (10.1, server 9.5.10).

    You get 58, not some value less than 30.
    Remove age() function, solved the problem that previous post mentioned.

    drop table t;
    create table t(
        d1 date
    );
    
    insert into t values(current_date - interval '58 day');
    
    select d1
    , current_timestamp - d1::timestamp date_diff
    , date_part('day', current_timestamp - d1::timestamp)
    from t;
    
         d1     |        date_diff        | date_part
    ------------+-------------------------+-----------
     2018-05-21 | 58 days 21:41:07.992731 |        58
    

提交回复
热议问题