Received: from maia.hub.org (maia-3.hub.org [200.46.204.243]) by mail.postgresql.org (Postfix) with ESMTP id 2E537B5DBD1 for ; Sat, 11 Jun 2011 02:11:46 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.243]) (amavisd-maia, port 10024) with ESMTP id 46524-07 for ; Sat, 11 Jun 2011 05:11:39 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from mail-pz0-f46.google.com (mail-pz0-f46.google.com [209.85.210.46]) by mail.postgresql.org (Postfix) with ESMTP id BF17DB5DBC1 for ; Sat, 11 Jun 2011 02:11:38 -0300 (ADT) Received: by pzk9 with SMTP id 9so1504647pzk.19 for ; Fri, 10 Jun 2011 22:11:37 -0700 (PDT) MIME-Version: 1.0 Received: by 10.68.27.130 with SMTP id t2mr1345774pbg.230.1307769097439; Fri, 10 Jun 2011 22:11:37 -0700 (PDT) Received: by 10.68.49.197 with HTTP; Fri, 10 Jun 2011 22:11:37 -0700 (PDT) X-Originating-IP: [66.215.95.51] In-Reply-To: References: Date: Fri, 10 Jun 2011 22:11:37 -0700 Message-ID: Subject: Re: Returning a set of dates From: Samuel Gendler To: "C. Bensend" Cc: pgsql-sql@postgresql.org Content-Type: multipart/alternative; boundary=bcaec520eb6b8049d904a568b82d X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-1.899 tagged_above=-5 required=5 tests=BAYES_00=-1.9, HTML_MESSAGE=0.001 X-Spam-Level: X-Archive-Number: 201106/32 X-Sequence-Number: 35498 --bcaec520eb6b8049d904a568b82d Content-Type: text/plain; charset=ISO-8859-1 On Fri, Jun 10, 2011 at 9:28 PM, C. Bensend 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 --bcaec520eb6b8049d904a568b82d Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable
On Fri, Jun 10, 2011 at 9:28 PM, C. Bensend <benny@bennyvision.com> w= rote:
SELECT next_bill_date( '2011-01-01', '1 month', '4 mont= hs' );


.. I expect the following result set:


=A0next_bill_date
----------------

2011-07-01
2011-08-01
2011-09-01
2011-10-01






CREATE OR REPLACE FUNCTION public.next_bill_date(d date= , period interval,
=A0=A0i interval)
=A0RETURNS SETOF date
AS $functi= on$
DECLARE
=A0=A0 =A0max_date date;
=A0=A0 = =A0due_date date;
BEGIN
=A0=A0 =A0max_date :=3D CURRENT= _DATE + i;
=A0=A0 =A0due_date :=3D d;
=A0=A0 =A0WHILE due_date + period <=3D max_date LOOP
=A0= =A0 =A0 =A0 =A0RETURN NEXT due_date; -- add d to the result set
= =A0=A0 =A0 =A0 =A0due_date :=3D due_date + period;
=A0=A0 =A0END = LOOP;
=A0=A0 =A0RETURN; -- exit function
END;
$function$ language plpgsql;

<= br>
testdb=3D# select next_bill_date('2011-06-11', &= #39;2 week', '3 month'); next_bill_date=A0
----------= ------
=A02011-06-11
=A02011-06-25
=A02011-07-09
=A02011-07-23
=A02011-08-06
=A02011-08-20
=

--bcaec520eb6b8049d904a568b82d--