Re: Returning a set of dates

From: Samuel Gendler <sgendler(at)ideasculptor(dot)com>
To: "C(dot) Bensend" <benny(at)bennyvision(dot)com>
Cc: pgsql-sql(at)postgresql(dot)org
Subject: Re: Returning a set of dates
Date: 2011-06-11 05:11:37
Message-ID: BANLkTik2c8U=DyFAkDDFDNaiagcrspYrdQ@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

On Fri, Jun 10, 2011 at 9:28 PM, C. Bensend <benny(at)bennyvision(dot)com> wrote:

> SELECT next_bill_date( '2011-01-01', '1 month', '4 months' );
>
>
> .. I expect the following result set:
>
>
> next_bill_date
> ----------------
>
> 2011-07-01
> 2011-08-01
> 2011-09-01
> 2011-10-01
>
>
>

http://www.postgresql.org/docs/8.4/interactive/plpgsql-control-structures.html#PLPGSQL-STATEMENTS-RETURNING

CREATE OR REPLACE FUNCTION public.next_bill_date(d date, period interval,
i interval)
RETURNS SETOF date
AS $function$
DECLARE
max_date date;
due_date date;
BEGIN
max_date := CURRENT_DATE + i;
due_date := d;
WHILE due_date + period <= max_date LOOP
RETURN NEXT due_date; -- add d to the result set
due_date := due_date + period;
END LOOP;
RETURN; -- exit function
END;
$function$ language plpgsql;

testdb=# select next_bill_date('2011-06-11', '2 week', '3 month');
next_bill_date
----------------
2011-06-11
2011-06-25
2011-07-09
2011-07-23
2011-08-06
2011-08-20

In response to

Browse pgsql-sql by date

  From Date Subject
Next Message Jasen Betts 2011-06-11 09:59:42 Re: ANY for Array value check
Previous Message C. Bensend 2011-06-11 04:28:04 Returning a set of dates