From: Samuel Gendler <sgendler@ideasculptor.com>
To: C. Bensend <benny@bennyvision.com>
Cc: pgsql-sql@postgresql.org
Subject: Re: Returning a set of dates
Date: Fri, 10 Jun 2011 22:11:37 -0700
Message-ID: <BANLkTik2c8U=DyFAkDDFDNaiagcrspYrdQ@mail.gmail.com> (raw)
In-Reply-To: <c29311b814cfdc327e4b19ae910d8a93.squirrel@webmail.stinkweasel.net>
References: <c29311b814cfdc327e4b19ae910d8a93.squirrel@webmail.stinkweasel.net>
On Fri, Jun 10, 2011 at 9:28 PM, C. Bensend <benny@bennyvision.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-RE...
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
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients using the --to and --cc options:
reply via email
To: pgsql-sql@postgresql.org
Cc: sgendler@ideasculptor.com, benny@bennyvision.com
Subject: Re: Returning a set of dates
In-Reply-To: <BANLkTik2c8U=DyFAkDDFDNaiagcrspYrdQ@mail.gmail.com>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox