From: | Pavel Stehule <pavel(dot)stehule(at)gmail(dot)com> |
---|---|
To: | Enzen user <anushka(dot)achaiah(at)enzentech(dot)com> |
Cc: | pgsql-sql(at)postgresql(dot)org |
Subject: | Re: sorting months according to fiscal year |
Date: | 2011-08-22 13:39:52 |
Message-ID: | CAFj8pRCjv=sOaDFjyxNBPPN79fhgjk8QAaVGzEhYbKYYiWTuSw@mail.gmail.com |
Views: | Raw Message | Whole Thread | Download mbox | Resend email |
Thread: | |
Lists: | pgsql-sql |
Hello
2011/8/22 Enzen user <anushka(dot)achaiah(at)enzentech(dot)com>:
> 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 $$
you cannot use to_number function inside param's list
> 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;
>
CREATE OR REPLACE FUNCTION sort_month(int)
RETURNS int -- is wrong to use numeric here
AS $$
...
$$ LANGUAGE sql;
SELECT sort_ month(to_nuber(...))
Regards
Pavel Stehule
>
>
> --
> 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.
>
> --
> Sent via pgsql-sql mailing list (pgsql-sql(at)postgresql(dot)org)
> To make changes to your subscription:
> http://www.postgresql.org/mailpref/pgsql-sql
>
From | Date | Subject | |
---|---|---|---|
Next Message | JavaNoobie | 2011-08-22 14:38:22 | Confused about writing this stored procedure/method. |
Previous Message | Enzen user | 2011-08-22 13:27:13 | sorting months according to fiscal year |