PostgreSQL: days/months/years between two dates

后端 未结 10 2008
旧时难觅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条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-12 20:30

    @WebWanderer 's answer is very close to the DateDiff using SQL server, but inaccurate. That is because of the usage of age() function.

    e.g. days between '2019-07-29' and '2020-06-25' should return 332, however, using the age() function it will returns 327. Because the age() returns '10 mons 27 days" and it treats each month as 30 days which is incorrect.

    You shold use the timestamp to get the accurate result. e.g.

    ceil((select extract(epoch from (current_date::timestamp - ::timestamp)) / 86400))

提交回复
热议问题