Reputation: 25564
I need to create postgresql function
CREATE FUNCTION date_ranges (_start date, end date)
RETURNING TABLE(day_in_range date) AS...
if I call date_ranges('2010-06-01', 2010-06-05') I should receive
2010-06-01
2010-06-02
2010-06-03
2010-06-04
2010-06-05
Any Ideas how to do it?
Upvotes: 2
Views: 1084
Reputation: 7705
If you're on Postgresql 8.4:
SELECT generate_series(_start ::timestamp,_end ::timestamp,'1 day');
Example:
postgres=# SELECT generate_series('2010-06-01'::timestamp,
postgres-# '2010-06-05'::timestamp,'1 day')::date;
generate_series
-----------------
2010-06-01
2010-06-02
2010-06-03
2010-06-04
2010-06-05
On older versions:
SELECT '2010-06-01'::date + step FROM
generate_series(0,'2010-06-05'::date - '2010-06-01'::date,1) AS t(step);
Upvotes: 5