2

I have a query which gets month name from a table. But this column isn't a datetime data type, It's a varchar column. How can I sort it according to the month name in ascending order?
This is the output I get at the moment.

August
November
October
September

This is my query

select distinct(payemnt_month) as month from payement_details
Mike
  • 1,017
  • 4
  • 19
  • 34

3 Answers3

3

Use below one i have appended your month with day and year. Then i am extracting month number

select * from payment_details order by DATEPART(MM,payemnt_month+'01'+'00')

Update If possible update your query like below.

SELECT * FROM 
(
SELECT DISTINCT month,
                Datepart(MM, payemnt_month+ '01' + '00') MONTHNO
FROM   payment_details )A
ORDER  BY MONTHNO

Or Like below if you don't have any issue to keep month no

   SELECT DISTINCT month,
                        Datepart(MM, payemnt_month+ '01' + '00') MONTHNO
        FROM   payment_details
order by month,Datepart(MM, payemnt_month+ '01' + '00')
Tharunkumar Reddy
  • 2,773
  • 18
  • 32
2
ORDER BY CASE WHEN payment_month='August' THEN 8
              WHEN payment_month='November' THEN 11
              WHEN payment_month='October' THEN 10
              WHEN payment_month='September' THEN 9 END

Frame your ORDER BY clause as above and add remaining months into it as required.

Akshey Bhat
  • 8,227
  • 1
  • 20
  • 20
0

Just put order by clause as:

select distinct(payemnt_month) as month from payement_details order by payemnt_month
Rahul Maurya
  • 187
  • 5