在SQL Server 2012中,可以使用以下方法将5分钟间隔设置为datetime:
SELECT DATEADD(MINUTE, 5, GETDATE()) AS NextDateTime;
这将返回当前日期和时间加上5分钟后的结果。
DECLARE @CurrentDateTime DATETIME;
SET @CurrentDateTime = GETDATE();
DECLARE @MinutesToAdd INT;
SET @MinutesToAdd = 5 - (DATEPART(MINUTE, @CurrentDateTime) % 5);
SELECT DATEADD(MINUTE, @MinutesToAdd, @CurrentDateTime) AS NextDateTime;
这将返回当前日期和时间加上满足5分钟间隔的下一个时间。
DECLARE @CurrentDateTime DATETIME;
SET @CurrentDateTime = GETDATE();
DECLARE @MinutesToAdd INT;
SET @MinutesToAdd =
CASE
WHEN DATEPART(MINUTE, @CurrentDateTime) < 5 THEN 5 - DATEPART(MINUTE, @CurrentDateTime)
WHEN DATEPART(MINUTE, @CurrentDateTime) < 10 THEN 10 - DATEPART(MINUTE, @CurrentDateTime)
WHEN DATEPART(MINUTE, @CurrentDateTime) < 15 THEN 15 - DATEPART(MINUTE, @CurrentDateTime)
WHEN DATEPART(MINUTE, @CurrentDateTime) < 20 THEN 20 - DATEPART(MINUTE, @CurrentDateTime)
WHEN DATEPART(MINUTE, @CurrentDateTime) < 25 THEN 25 - DATEPART(MINUTE, @CurrentDateTime)
WHEN DATEPART(MINUTE, @CurrentDateTime) < 30 THEN 30 - DATEPART(MINUTE, @CurrentDateTime)
WHEN DATEPART(MINUTE, @CurrentDateTime) < 35 THEN 35 - DATEPART(MINUTE, @CurrentDateTime)
WHEN DATEPART(MINUTE, @CurrentDateTime) < 40 THEN 40 - DATEPART(MINUTE, @CurrentDateTime)
WHEN DATEPART(MINUTE, @CurrentDateTime) < 45 THEN 45 - DATEPART(MINUTE, @CurrentDateTime)
WHEN DATEPART(MINUTE, @CurrentDateTime) < 50 THEN 50 - DATEPART(MINUTE, @CurrentDateTime)
WHEN DATEPART(MINUTE, @CurrentDateTime) < 55 THEN 55 - DATEPART(MINUTE, @CurrentDateTime)
ELSE 60 - DATEPART(MINUTE, @CurrentDateTime)
END;
SELECT DATEADD(MINUTE, @MinutesToAdd, @CurrentDateTime) AS NextDateTime;
这将返回当前日期和时间加上满足5分钟间隔的下一个时间。
腾讯云相关产品和产品介绍链接地址:
领取专属 10元无门槛券
手把手带您无忧上云