检查当前时间是否在SQL中的两个不同时间之间

问题描述 投票:0回答:1

我想用 SQL 检查当前时间是否在晚上 10 点到[凌晨 4 点(第二天)]之间。

DECLARE @tt TIME
SET @tt = CONVERT(TIME, SYSDATETIMEOFFSET())

--

2023-09-12 19:19:35.4692966 -05:00

这将为我提供当前时间,但我如何检查它是否在今天晚上 10 点到第二天凌晨 4 点之间?

sql datetime time datetimeoffset datepart
1个回答
0
投票

要检查 SQL Server 中的当前时间是否在晚上 10 点到第二天凌晨 4 点之间,可以使用以下 SQL 查询:

DECLARE @CurrentTime TIME = GETDATE();  -- Get the current time

IF @CurrentTime >= '22:00:00' OR @CurrentTime < '04:00:00'
BEGIN
    -- Current time is between 10 PM and 4 AM (the next day)
    PRINT 'Current time is within the specified range.'
END
ELSE
BEGIN
    -- Current time is not within the specified range
    PRINT 'Current time is outside the specified range.'
END
© www.soinside.com 2019 - 2024. All rights reserved.