views:

44

answers:

1

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?

+3  A: 

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);
rfusca
Works perfect - THANK YOU (specially for older version I use 8.1) - bensiu
bensiu
8.1 will be EOL this year, start upgrading to a newer version.
Frank Heikens