Sort month that is in extensions in Postgres

6

I need to make a ORDER BY in a table mes that has the months in extenso and in Portuguese (Janeiro, Fevereiro...) . It has no relation to any other column in the table. Is it possible to do this?

    
asked by anonymous 20.05.2015 / 15:57

2 answers

4

I've been investigating and I think I've found a solution for you, passing every% of% of the month to its number (eg string ):

SELECT 
       CASE 
            WHEN mes = "Janeiro" THEN 1
            WHEN mes = "Fevereiro" THEN 2
            WHEN mes = "Março" THEN 3
            WHEN mes = "Abril" THEN 4
            ...
       END as meses
       , mes //apresentar também o mês em string ao lado do número
FROM tbl_meses;
ORDER BY meses ASC
    
20.05.2015 / 16:26
1

If this table is just to translate the months a simple solution is to add a column with month number the ordering should be done by the year and by that new column.

If you have a date field or similar and want to display the month in full in Portuguese you can set the locale in the session and sort by month.

set lc_time = 'portuguese';
select to_char(data,'tmmonth') from tabela order by extract(year from data), extract(month from data)
    
20.05.2015 / 17:26