Received: from maia.hub.org (maia-5.hub.org [200.46.204.29]) by mail.postgresql.org (Postfix) with ESMTP id ADC6DB5DBFB for ; Mon, 22 Aug 2011 20:58:38 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.29]) (amavisd-maia, port 10024) with ESMTP id 82415-06 for ; Mon, 22 Aug 2011 23:58:32 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from mbx.knossos.net.nz (mbx.knossos.net.nz [202.160.48.10]) by mail.postgresql.org (Postfix) with ESMTP id AD7BFB5DBFA for ; Mon, 22 Aug 2011 20:58:30 -0300 (ADT) Received: from [10.1.1.3] (60-234-150-59.bitstream.orcon.net.nz [60.234.150.59]) (authenticated bits=0) by mbx.knossos.net.nz (8.14.4/8.14.4) with ESMTP id p7MNwFLg030007 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=NOT); Tue, 23 Aug 2011 11:58:21 +1200 Message-ID: <4E52ED17.9070204@archidevsys.co.nz> Date: Tue, 23 Aug 2011 11:58:15 +1200 From: Gavin Flower Organization: ArchiDevSys User-Agent: Mozilla/5.0 (X11; Linux i686 on x86_64; rv:6.0) Gecko/20110812 Thunderbird/6.0 MIME-Version: 1.0 To: Enzen user CC: pgsql-sql@postgresql.org Subject: Re: sorting months according to fiscal year References: <1314019633187-4723409.post@n5.nabble.com> In-Reply-To: <1314019633187-4723409.post@n5.nabble.com> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-0.778 tagged_above=-5 required=5 tests=BAYES_00=-1.9, URI_HEX=1.122 X-Spam-Level: X-Archive-Number: 201108/89 X-Sequence-Number: 35757 On 23/08/11 01:27, Enzen user wrote: > Hi > I have to rearrange the months according to the fiscal year i.e from April > to march and use the same in the order by clause of a query. > I have written the following postgresql function for the same, but to_number > is returning an error. > Can you please tell me where i'm going wrong? > Instead of the function to_number can you suggest any other function that > will convert a particular month to its corresponding month number(ex: > april=4 or jan=1) > > > CREATE FUNCTION sort_month(to_number('April','MM')) RETURNS numeric AS $$ > DECLARE > > BEGIN > > CASE WHEN 4 THEN 1 > WHEN 5 THEN 2 > WHEN 6 THEN 3 > WHEN 7 THEN 4 > WHEN 8 THEN 5 > WHEN 9 THEN 6 > WHEN 10 THEN 7 > WHEN 11 THEN 8 > WHEN 12 THEN 9 > WHEN 1 THEN 10 > WHEN 2 THEN 11 > WHEN 3 THEN 12 > ELSE 0 > END; > > > > $$ LANGUAGE plpgsql; > > > > -- > View this message in context: http://postgresql.1045698.n5.nabble.com/sorting-months-according-to-fiscal-year-tp4723409p4723409.html > Sent from the PostgreSQL - sql mailing list archive at Nabble.com. > You might find the following faster... DROP FUNCTION IF EXISTS sort_mont ( to_number int ) ; CREATE FUNCTION sort_mont ( to_number int ) RETURNS numeric LANGUAGE plpgsql AS $$ BEGIN RETURN 1 + (to_number + 8) % 12; END; $$; SELECT sort_mont(1); SELECT sort_mont(12); /// output.. gavin=> \i modulus_stored_proc.sql DROP FUNCTION CREATE FUNCTION sort_mont ----------- 10 (1 row) sort_mont ----------- 9 (1 row) gavin=>