Yousuf Sultan
Yousuf Sultan

Reputation: 3257

Order by using month name in PostgreSQL

I have a table which has a field Month_Name and it contains the names of the month. I want to ORDER BY the month names, not alphabetically, but by its actual order, like, January, February, etc. How can I implement this using PostgreSQL?

Is there any way that I can convert the Month Name to its numeric value?

id        billed_unit    billed_amount    town_id    ea_month    ea_year    
3959920   3695.17        25856.84         CHRY     April         2014
3959920   3695.17        25856.84         CHRY     August        2014
3959920   3695.17        25856.84         CHRY     February      2014
3959920   3695.17        25856.84         CHRY     July          2014
3959920   3695.17        25856.84         CHRY     June          2014
3959920   3695.17        25856.84         CHRY     March         2014

Upvotes: 6

Views: 10425

Answers (1)

Vivek S.
Vivek S.

Reputation: 21995

SELECT * 
FROM EA.TOWN_CONS_BILLING_ROLLUP 
WHERE TOWN_ID='CHRY' 
      AND EA_YEAR=2014 
ORDER BY   
to_date(ea_month,'Month');

Data Type Formatting Functions

Upvotes: 12

Related Questions