我需要创建postgresql函数
CREATE FUNCTION date_ranges (_start date, end date)
RETURNING TABLE(day_in_range date) AS...
如果我拨打date_ranges('2010-06-01',2010-06-05') 我应该收到
2010-06-01
2010-06-02
2010-06-03
2010-06-04
2010-06-05
任何想法怎么做?
答案 0 :(得分:5)
如果您使用的是Postgresql 8.4:
SELECT generate_series(_start ::timestamp,_end ::timestamp,'1 day');
示例:
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
旧版本:
SELECT '2010-06-01'::date + step FROM
generate_series(0,'2010-06-05'::date - '2010-06-01'::date,1) AS t(step);