我有类型为time(7)[允许为空]的列,其中一些值需要调整为最接近的15分钟标记,而秒设置为零。
示例:
09:00:40.0000000 -> 09:00:00.0000000
12:50:00.0000000 -> 12:45:00.0000000
12:59:37.0000000 -> 13:00:00.0000000
答案 0 :(得分:0)
试试这个-
SELECT
CAST
(
DATEADD(MINUTE, ROUND(DATEDIFF(MINUTE, 0,
CAST('2015-11-24 ' -- Add any day for converting to DateTime
+LEFT('09:00:40.0000000',LEN('09:00:40.0000000') - 4) -- Removing last four zero from your data to convert to date time
AS DATETIME)) / 15.0, 0) * 15, 0)
AS TIME
);
答案 1 :(得分:0)
我推荐timefromparts()
:
select timefromparts(datepart(hour, timecol),
(datepart(minute, timecol) / 15) * 15,
0, 0, 6)
以上内容将值截断。四舍五入值有点麻烦。我建议:
select timefromparts(datepart(hour, dateadd(second, 7 * 60 + 30, timecol)),
(datepart(minute, dateadd(second, 7 * 60 + 30, timecol)) / 15) * 15,
0, 0, 6)
也就是说,添加7.5分钟并截断。