PostgreSQL: days/months/years between two dates

后端 未结 10 1866
旧时难觅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:40

    SELECT
      AGE('2012-03-05', '2010-04-01'),
      DATE_PART('year', AGE('2012-03-05', '2010-04-01')) AS years,
      DATE_PART('month', AGE('2012-03-05', '2010-04-01')) AS months,
      DATE_PART('day', AGE('2012-03-05', '2010-04-01')) AS days;
    

    This will give you full years, month, days ... between two dates:

              age          | years | months | days
    -----------------------+-------+--------+------
     1 year 11 mons 4 days |     1 |     11 |    4
    

    More detailed datediff information.

提交回复
热议问题