如何用最接近的值舍入时间

时间:2012-06-06 10:08:33

标签: sql sql-server sql-server-2000

使用SQL Server 2000

例如

表1

time

01:02
00:02
02:37
02:58
....

时间格式为HH:MM

我想用最接近的值来计算时间。像

01 to 04 means 00 only, 05 to 09 means 05 only, 10 to 14 means 10 only..... 55 to 59 means 55 only, 01:00 to 01:04 means 01:00 only....

预期产出

表1

time

01:00
00:00
02:35
02:55
....

如何查询上述情况。

需要查询帮助

2 个答案:

答案 0 :(得分:4)

select substring([time], 1, 3) + 
       right(rtrim(('0' + cast(substring([time], 4, 2) / 5 * 5 as CHAR))), 2)
from table1

答案 1 :(得分:1)

SELECT
  CONVERT(
    char(5),
    DATEADD(MINUTE, DATEDIFF(MINUTE, 0, time) / 5 * 5, 0),
    108
  ) AS time
FROM Table1