Received: from localhost (postgresql.org [64.49.215.8]) by postgresql.org (Postfix) with ESMTP id 42BD64764F9 for ; Wed, 25 Sep 2002 10:38:03 -0400 (EDT) Received: from news.hub.org (news.hub.org [64.49.215.80]) by postgresql.org (Postfix) with ESMTP id 58EF7476424 for ; Wed, 25 Sep 2002 10:38:02 -0400 (EDT) Received: by news.hub.org (Postfix, from userid 8) id 2924B3817A6; Wed, 25 Sep 2002 10:38:05 -0400 (EDT) From: Dmitry Tkach X-Newsgroups: comp.databases.postgresql.sql Subject: Re: Is there a better way than this to get the start and end of a month? Date: Wed, 25 Sep 2002 10:28:08 -0400 Organization: Hub.Org Networking Services (http://www.hub.org) Lines: 47 Message-ID: <3D91C7F8.9000303@openratings.com> References: <1032672769.21707.62.camel@ciderbox> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii; format=flowed Content-Transfer-Encoding: 7bit X-Complaints-To: news@hub.org To: David Stanaway User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:0.9.7+) Gecko/20020123 X-Accept-Language: en-us To: pgsql-sql@postgresql.org X-Virus-Scanned: by AMaViS new-20020517 X-Archive-Number: 200209/314 X-Sequence-Number: 9591 Well, month_end could be more straightforward as something like select month_start ($1) + interval '1 day' - interval '1 month'; Dima David Stanaway wrote: > Here are the 2 functions I have at the moment. I was wondering if > someone had a better way? > > CREATE OR REPLACE FUNCTION month_start (date) > RETURNS date > AS ' > DECLARE > day ALIAS FOR $1; > BEGIN > RETURN day - (extract(''day'' FROM day)||'' days'')::interval + > ''1 day''::interval; > END; > ' > LANGUAGE 'plpgsql'; > > CREATE OR REPLACE FUNCTION month_end (date) > RETURNS date > AS ' > DECLARE > day ALIAS FOR $1; > month int; > year int; > BEGIN > month := extract(''month'' FROM day); > year := extract(''year'' FROM day); > IF month = 12 THEN > month := 1; > year := year +1; > ELSE > month := month +1; > END IF; > RETURN (''01-''||month||''-''||year)::date - > ''1 day''::interval; > END; > ' > LANGUAGE 'plpgsql'; > >