在 MSSQL 2012 上使用纯 TSQL 确定当前时间是否在本小时的第 59 分钟和下一小时的第一分钟之间的智能方法是什么?
我不想从“现在”截断时间,我想截断日期,只做一个时间计算。
这让我有点接近:Datepart for time between (而不是 Convert date)但不完全是因为我需要根据当前时间而不是表值进行计算。
以下可能有助于说明服务器当前是否处于“糟糕时间”并且应该返回值“1”
14:58:59 , Should not be considered in the "bad time"
14:59:00 , Should be considered in the "bad time"
14:59:04 , Should be considered in the "bad time"
14:59:57 , Should be considered in the "bad time"
15:00:02 , Should be considered in the "bad time"
15:01:26 , Should not be considered in the "bad time"
15:02:00 , Should not be considered in the "bad time"
15:02:01 , Should not be considered in the "bad time"
谢谢。
编辑:
这段代码有效,但我相信这可以更有效地完成。欢迎所有建议。再次感谢。
declare @now time = ( select cast( sysutcdatetime() as time ))
declare @floor time = ( select dateadd( minute , 59 , ( dateadd(hour,datediff(hour,0,@now),0) ) ) )
declare @ceiling time =( select dateadd( minute , 2 , @floor ) )
if ( @now > @floor and @now < @ceiling )
begin
select 1 as bit
end
select @now , @floor , @ceiling