简体   繁体   中英

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?

DATENAME returns a string.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM