Check if current time is between two different times in SQL

39 views Asked by At

I want to check if the current time is between 10 PM and [4 AM (the next day)] in SQL.

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

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

This will get me the current time, but how can I check if it is between 10 PM today and 4 AM the next day?

1

There are 1 answers

0
Ammad Amir On BEST ANSWER

To check if the current time is between 10 PM and 4 AM (the next day) in SQL Server, you can use the following SQL query:

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