简体   繁体   English

SQL Server:按时间顺序排列Datetime2时间戳

[英]SQL Server : order by time portion of Datetime2 timestamp

I have a table of orders with a datetime2(7) column O_CreatedOn and I would like to order this recordset by the time portion only of the O_CreatedOn column. 我有一个带有datetime2(7)O_CreatedOn的订单表,我想按O_CreatedOn列的时间部分订购此记录集。

The reason for this is that I need to generate a report that shows the most prevalent times of day when orders are taking place. 这样做的原因是我需要生成一份报告,显示订单发生时最普遍的时间。

Due to the way orders have been stored in the table and the volume of orders, I do not want to break the timestamp column O_CreatedOn into O_CreatedOn_Date and O_CreatedOn_Time columns. 由于订单已存储在表中的方式和订单量,我不想将时间戳列O_CreatedOn分解为O_CreatedOn_DateO_CreatedOn_Time列。

I tried ordering the recordset by a converted O_CreatedOn but it's still sorting on the full timestamp. 我尝试通过转换后的O_CreatedOn对记录集进行排序,但它仍在整个时间戳上排序。

So, this: 所以这:

ORDER BY CONVERT(datetime, O_CreatedOn, 108)

did not work. 不工作。

你可以转换成time

ORDER BY CONVERT(time, O_CreatedOn) 

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

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