Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 914
Mark Avatar asked Aug 22 '26 18:08

Mark


1 Answers

DATENAME returns a string.

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

like image 147
Martin Smith Avatar answered Aug 25 '26 08:08

Martin Smith



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!