开发者

Generating Table / array on the fly in postgresql function

开发者 https://www.devze.com 2023-01-03 22:49 出处:网络
I need to create postgresql function CREATE FUNCTION date_ranges (_start date, end d开发者_如何转开发ate)

I need to create postgresql function

CREATE FUNCTION date_ranges (_start date, end d开发者_如何转开发ate) 
  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?


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);
0

精彩评论

暂无评论...
验证码 换一张
取 消