我有这样的表
Id Student Test_Start Test_End
1 A 2013-01-08 15:00:17.100 2013-01-08 16:05:55.100
2 B 2013-02-08 17:01:11.800 2013-02-08 19:05:15.880
3 C 2013-02-08 14:41:10.880 2013-02-08 16:49:22.880
我要做的是获取Test_End - Test_Start时间并将其更改为小时并按升序显示。我试过了:
Select convert(date, Test_End) - convert(date,Test_Start) as 'Time_Taken' from students
或尝试使用DATEDIFF但似乎无法使其正常工作。是否有最好的方法来实现这一目标:
Time_Taken
1
2
2
答案 0 :(得分:4)
select
id,
student,
datediff(hour, test_start, test_end) as time_taken
from students
order by time_taken
如果你想要四舍五入,确定分钟数,加上四舍五入到下一个小时所需的分钟数,再除以60得到舍入小时......
select
id,
student,
(datediff(minute, test_start, test_end) + 30) / 60 as rounded_time_taken
from students
order by rounded_time_taken
答案 1 :(得分:1)
你真的想要整数小时吗?在大多数情况下,我宁愿将结果视为小时数:
select id, student, datediff(s, test_start, test_end)/(60.0*60.0) as hours_taken
from students
order by test_start, test_end)/(60.0*60.0)