2

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?

1

1 Answer 1

5

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);
Sign up to request clarification or add additional context in comments.

2 Comments

Works perfect - THANK YOU (specially for older version I use 8.1) - bensiu
8.1 will be EOL this year, start upgrading to a newer version.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.