简体   繁体   English

SQL SORT BY DATENAME不起作用

[英]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. DATENAME返回一个字符串。

Use DATEPART(DD,s.requestdate) instead as that returns an integer and will sort as you expect. 使用DATEPART(DD,s.requestdate)代替返回一个整数,并按预期排序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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