If I am given a date like 1999-07-08 15:49:00 what would be a good function to determine whether is an AM shift, PM shift or a NOC shift?
--AM: 06:45:00 - 14:44:59
--PM: 14:45:00 - 22:59:59
--NOC: 23:00:00 - 06:44:59
Here is my attempt but then I noticed a bug
ALTER FUNCTION [dbo].[DateToNocShift]
(
-- Add the parameters for the function here
@DummyDate DATETIME
)
RETURNS VARCHAR(10)
AS
BEGIN
-- Declare the return variable here
DECLARE @Shift VARCHAR(10)
DECLARE @DateValues TABLE
(
RawDate DATETIME,
HourNow int,
MinuteNow int,
TimeHourMinute FLOAT,
Shift VARCHAR(4)
)
INSERT INTO @DateValues
VALUES
(
@DummyDate,
DATEPART(hour,@DummyDate),
cast(DATEPART(minute,@DummyDate)as decimal),
ROUND(DATEPART(hour,@DummyDate) + cast(DATEPART(minute,@DummyDate)as decimal)/60,2),
null
)
UPDATE @DateValues
SET Shift = 'AM'
WHERE TimeHourMinute BETWEEN 6.75 AND 14.74 -- good estimate
UPDATE @DateValues
SET Shift = 'PM'
WHERE TimeHourMinute BETWEEN 14.75 AND 22.99
UPDATE @DateValues
SET Shift = 'NOC'
WHERE TimeHourMinute BETWEEN 23.00 AND 6.74
SELECT @Shift = Shift FROM @DateValues
RETURN @Shift