Rachel
Rachel

Reputation: 103577

Oracle: How to sort the query to get months in proper sequence

I am working on an report for which am calling a function from front end. I am getting start month information as numbers starting from 1 to 12 and so am using

substr(to_date(START_MONTH,'MM'),4,3)

to get month in the form of Jan, Feb etc, now in my function am grouping by start month and then ordering by start month and so in the output am getting values like

Apr, Aug, Dec as it is ordering by First alphabet of the month, how can I get correct order starting from Jan to Dec?

Any suggestions?

Also how to sort number so that 2 comes after 1 and not 10 after 1?

Upvotes: 1

Views: 15072

Answers (2)

coolpanchdev
coolpanchdev

Reputation: 1

Try this query

select TO_CHAR(T1.SCHDATE, 'Mon-YY') FROM FINSCHEDULEDETAILS T1  
 GROUP BY TO_CHAR(T1.SCHDATE, 'Mon-YY'),TO_CHAR(T1.SCHDATE, 'YYMM')
    ORDER BY TO_CHAR(T1.SCHDATE, 'YYMM'));

Upvotes: 0

Randy
Randy

Reputation: 16673

order by to_date( start_month, 'mm' )

?

Upvotes: 6

Related Questions