SQL Server- Check if Current Time is in Standard Work Week

Had to figure this out today, and thought I’d post it here, because I didn’t see it anywhere else out there.
There maybe better ways of doing this. If so, please let me know.

--Check to see if current time is a weekday and between 9 and 5
DECLARE @isWorkHours AS bit

SELECT @isWorkHours = COUNT(1)
WHERE
--Monday is 2 and Friday is 6.
DATEPART(dw,GETDATE()) BETWEEN 2 AND 6
AND
--This gets times between 9:00 and 4:59
DATEPART(hour, GETDATE()) BETWEEN 9 AND 16
IF @isWorkHours = 1
BEGIN
SELECT 1
END

IF @isWorkHours = 0
BEGIN
SELECT 0
END

2 Responses to SQL Server- Check if Current Time is in Standard Work Week

Leave a Reply

Your email address will not be published. Required fields are marked *