0

在 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
4

3 回答 3

3
IF DATEPART(MINUTE, SYSDATETIME()) = 59
    PRINT '59th minute';
于 2013-04-19T20:16:17.040 回答
1

分钟(@dt)=59 或分钟(@dt)=0

于 2013-04-19T21:37:57.167 回答
1

请根据需要更换@dt

declare @dt datetime = '20130419 21:59:01'

select case when datepart(minute, @dt) in (59,0)
            then 'Minute is between 59 and 1'
            else 'Minute is NOT between 59 and 1'
       end myMinute
于 2013-04-19T21:16:54.497 回答