0

I am trying to order by Date in SQL Server I am facing a weird issue. There are 2 cases I should explain to help you understand my issue .

Case 1 :

select [MonthName] 
from prod.[dim date] 
where F_Year = 2014   
order by [Date]

My output:

june june june july july july july august  
// Here I get duplicates but order by is working as expected 

Case 2 : tried to remove duplicates by using "distinct"

select distinct [MonthName] 
from prod.[dim date] 
where F_Year = 2014   
order by [Date]

My output:

August july june 
// Order By not working as expected (ordering alphabetical wise ) . 

Any workaround is appreciated

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
super cool
  • 6,003
  • 2
  • 30
  • 63

1 Answers1

0

Firstly your query seems to be working off a table with 3 fields representing possibly a single date or maybe 2 - hard to tell without the structure but I can see : F_Year, [Date] & [MonthName]. So if these actually represent a single date, then revert to that single date and use formatting to determine F_Year and MonthName.

Secondly check out these posts for a probable answer to your question:

Sort by Date in SQL

Convert Month Number to Month Name Function in SQL

Community
  • 1
  • 1
simo.3792
  • 2,102
  • 1
  • 17
  • 29