Writing a function in SQL to loop through a date range in a UDF
No need for functions:
select dhcp.singleday(a::date, a::date + 1)from generate_series( '2012-11-24'::date, '2012-12-03', '1 day') s(a)
This will work for any date range. Not only an inside month one.
Simple plpgsql function:
CREATE OR REPLACE FUNCTION f_machine_gun_sally(date, date) RETURNS void AS$func$DECLARE d date := $1;BEGINLOOP PERFORM dhcp.singleday(d, d+1); d := d + 1; EXIT WHEN d > $2;END LOOP;END$func$ LANGUAGE plpgsql;
- Use
PERFORM
when you want don't care about the return value. - You can just add an
integer
to adate
to increment. Month or year boundaries are irrelevant this way.