Order by month name in Chronological order

Database
Enthusiast

Order by month name in Chronological order

I had an interview question where i need to write a query to order by month name in chronological order. The table has just one column with 12 rows with 12 month names.

And It should be a general sql query with out using any functions.

3 REPLIES
Enthusiast

Re: Order by month name in Chronological order

any inputs ?

Enthusiast

Re: Order by month name in Chronological order

One way..

sel m from 
(
sel mnth,
case when mnth = 'jan' then 1
when mnth = 'feb' then 2
when mnth = 'mar' then 3
when mnth = 'apr' then 4
when mnth = 'may' then 5
when mnth = 'jun' then 6
when mnth = 'jul' then 7
when mnth = 'aug' then 8
when mnth = 'sept' then 9
when mnth = 'oct' then 10
when mnth = 'nov' then 11
when mnth = 'dec' then 12
end from Month_tab) M(m,o)
order by o;

Rglass

Enthusiast

Re: Order by month name in Chronological order

SELECT * FROM databasename.t_monthname 

ORDER BY month_name (DATE, FORMAT 'mmm')