Mark
Mark

Reputation: 891

SQL SORT BY DATENAME does not work

My query is

USE MyDB

SELECT DATENAME(DD, s.requestdate)
FROM   sample_table s
WHERE  MONTH(s.requestdate) = 12
GROUP  BY DATENAME(DD, s.requestdate)
ORDER  BY DATENAME(DD, s.requestdate) DESC 

The output I expect is to be ordered by day of month in descending order but my output is ordered such

9
8
 7....till 1 then 19, 18...till 11

What am I missing?

Upvotes: 3

Views: 1674

Answers (1)

Martin Smith
Martin Smith

Reputation: 452957

DATENAME returns a string.

Use DATEPART(DD,s.requestdate) instead as that returns an integer and will sort as you expect.

Upvotes: 8

Related Questions