Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Order By Month Name in Sql-Server

I have Below Table named session

SessionID   SessionName

100         August
101         September
102         October
103         November
104         December
105         January
106         May
107         June
108         July

I executed the following query I got the output as below.

Select SessionID, SessionName 
From dbo.Session  

SessionID   SessionName
100         August
101         September
102         October
103         November
104         December
105         January
106         May
107         June
108         July

the results get ordered by Session ID. But I need the output as below,

SessionID   SessionName 
106         May
107         June
108         July
100         August
101         September
102         October
103         November
104         December
105         January

How to achieve this in sql-server? thanks for the help

like image 927
bmsqldev Avatar asked Feb 27 '26 05:02

bmsqldev


1 Answers

I'd use a case expression, like:

order by case SessionName when 'August' then 1
                          when 'September' then 2
                          ...
                          when 'Juty' then 12
         end

August has 1 because "in the application logic a session started with august", easy to renumber if you want to start with January and end with December.

like image 117
jarlh Avatar answered Feb 28 '26 17:02

jarlh



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!