I keep hours of work in this table. Businesses may have several hours on the same day . Closing time may be after midnight of the current day.
CREATE TABLE [Process].[OpeningHours](
[openinghoursid] [int] IDENTITY(1,1) NOT NULL,
[businessid] [int] NOT NULL,
[daynumber] [int] NOT NULL,
[opentime] [time](7) NOT NULL,
[duration] [int] NOT NULL,
[closetime] AS (dateadd(minute,[duration],[opentime])
)
Examples of data in this table include:
INSERT [Process].[OpeningHours]
([openinghoursid], [businessid], [daynumber], [opentime], [duration])
VALUES (79, 18, 2, CAST(N'12:00:00' AS Time), 165),
(80, 18, 2, CAST(N'18:00:00' AS Time), 240),
(81, 18, 3, CAST(N'12:00:00' AS Time), 165),
(82, 18, 3, CAST(N'18:00:00' AS Time), 240),
(83, 18, 4, CAST(N'12:00:00' AS Time), 165),
(84, 18, 4, CAST(N'18:00:00' AS Time), 240),
(85, 18, 5, CAST(N'12:00:00' AS Time), 165),
(86, 18, 5, CAST(N'18:00:00' AS Time), 240),
(87, 18, 6, CAST(N'12:00:00' AS Time), 165),
(88, 18, 6, CAST(N'18:00:00' AS Time), 300),
(89, 18, 7, CAST(N'12:00:00' AS Time), 165),
(90, 18, 7, CAST(N'18:00:00' AS Time), 600),
(91, 18, 1, CAST(N'12:00:00' AS Time), 180);
Now I want to create a function that returns if the business is open or closed.
CREATE FUNCTION [Process].[ufnIsSpaceOpen](@businessid int)
RETURNS BIT
AS
BEGIN
DECLARE @currentdatetime DATETIME = GETDATE();
DECLARE @dayofweek INT = DATEPART(dw,@currentdatetime);
DECLARE @currentdate DATETIME = CONVERT(DATE, @currentdatetime);
DECLARE @isopen BIT;
SELECT @isopen = COUNT(*)
FROM Process.OpeningHours
WHERE
daynumber = @dayofweek
AND businessid = @businessid
AND
(
@currentdatetime >= @currentdate + CONVERT(DATETIME, opentime)
AND
@currentdatetime <=
CASE
WHEN closetime < '00:00:00' THEN @currentdate + CONVERT(DATETIME, closetime)
ELSE DATEADD(DAY,1,@currentdate) + CONVERT(DATETIME, closetime)
END
);
RETURN @isopen;
END;
GO
I use COUNT () to see if any of the lines matches the condition, if 0 matches, it means it is closed, if COUNT () is greater than 0, it is open. This works when the closing time is on the same day, however it does not work when the closing time is after midnight or the current time after midnight.
Any idea how I can fix this?
: . @DenisRubashkin. , , , :
CREATE FUNCTION [Process].[ufnIsSpaceOpen](@businessid int)
RETURNS BIT
AS
BEGIN
DECLARE @isopen BIT;
DECLARE @Date DATETIME = GETDATE();
SELECT @isopen = COUNT(*)
FROM
(
SELECT (CAST(CAST(@Date AS DATE) AS DATETIME) + CAST(h.opentime AS DATETIME)) AS Opened,
DATEADD(mi, h.duration, (CAST(CAST(@Date AS DATE) AS DATETIME) + CAST(h.opentime AS DATETIME))) AS Closed
FROM Process.OpeningHours h
WHERE h.daynumber = DATEPART(dw, @Date)
AND businessid = @businessid
UNION
SELECT (CAST(DATEADD(day, -1, CAST(@Date AS DATE)) AS DATETIME) + CAST(h.opentime AS DATETIME)) AS Opened,
DATEADD(mi, h.duration, (CAST(DATEADD(day, -1, CAST(@Date AS DATE)) AS DATETIME) + CAST(h.opentime AS DATETIME))) AS Closed
FROM Process.OpeningHours h
WHERE h.daynumber = CASE WHEN DATEPART(dw, @Date) = 1
THEN 7
ELSE DATEPART(dw, @Date) - 1
END
AND businessid = @businessid
) w
WHERE @Date BETWEEN Opened AND Closed
RETURN @isopen;
END;