09Q71AO534
09Q71AO534

Reputation: 4450

How to get the no of Days from the difference of two dates in PostgreSQL?

select extract(day from age('2013-04-06','2013-04-04'));`

gives me the no of days ! i.e.: 2 days

but it failed when I have a differnt month:

select extract(day from age('2013-05-02','2013-04-01'));

So what I need is to get the no of days as 32 days

Upvotes: 0

Views: 92

Answers (1)

Subtraction seems more intuitive.

select '2013-05-02'::date - '2013-04-01'::date

Run this query to see why the result is 31 instead of 32

with dates as (
  select generate_series('2013-04-01'::date, '2013-05-02'::date, '1 day')::date as end_date,
                         '2013-04-01'::date as start_date
)
select end_date, start_date, end_date - start_date as difference
from dates
order by end_date

The difference between today and today is zero days. Whether that matters is application-dependent. You can just add 1 if you need to.

Upvotes: 2

Related Questions