Your question is not quite clear. If you want to retrieve the
number of days in interval
You can use EXTRACT:
SELECT EXTRACT(day from '7 week'::interval) AS d
d
----
49
Note that this extracts a subfield of an interval as given. the interval is not normalized or justified. To justify the interval first use justify_interval()
SELECT EXTRACT(day from justify_interval('7 week'::interval)) AS d;
d
----
19
A month of 30 days has been deducted from the 49 days.
Accordingly these expressions return 0 days:
SELECT EXTRACT(day from '24h'::interval);
SELECT EXTRACT(day from '1 months'::interval);
If you just want the number of days that lie between dates:
SELECT '2012-3-23'::date - '2012-3-1'::date AS d;
d
----
22
You can just subtract dates to get an integer signifying the number of days in between.
Or between timestamps:
SELECT EXTRACT (day from '2012-03-25 23:12'::timestamp
- '2010-03-01 03:34'::timestamp) AS d;
d
-----
755
AVG()orSUM()for this purpose. So I kept an aggregate there. should I remove that from title ?