Missed to answer the last day, but still if you haven't solved the issue. I would suggest to give a try using union for your second shift data. As the day change in selecting between time range
/*******************************************************
--Commented Code
DECLARE @StartHour INT
DECLARE @EndHour INT
SET @StartHour = CASE @TimeRange
WHEN 0 THEN 0 --Start of Overall--
WHEN 1 THEN 6 --1st Shif Start--
WHEN 2 THEN 16 --2nd Shift Start--
END
SET @EndHour = CASE @TimeRange
WHEN 0 THEN 24 --End of Overall--
WHEN 1 THEN 16 --End of First Shift--
WHEN 2 THEN 3 --End of Second Shift--
END
*******************************************/
If(@TimeRange=1)
BEGIN
SELECT
Testerline1_CycleTimes.Station,
Testerline1_CycleTimes.StationEntry
WHERE
sterline1_CycleTimes.Station LIKE @Station
AND Testerline_CycleTimes.StationEntry Between @Start And @End
AND DATEPART( hh , Testerline1_CycleTimes.StationEntry ) >= 6
AND DATEPART( hh , Testerline1_CycleTimes.StationEntry ) < 16
END
ELSE IF(@TimeRange=2)
BEGIN
SELECT
Testerline1_CycleTimes.Station,
Testerline1_CycleTimes.StationEntry
WHERE
Testerline1_CycleTimes.Station LIKE @Station
AND Testerline1_CycleTimes.StationEntry Between @Start And @End
AND DATEPART( hh , Testerline1_CycleTimes.StationEntry ) >= 16
UNION ALL
SELECT
Testerline1_CycleTimes.Station,
Testerline1_CycleTimes.StationEntry
WHERE
Testerline1_CycleTimes.Station LIKE @Station
AND Testerline1_CycleTimes.StationEntry Between @Start And @End
AND DATEPART( hh , Testerline1_CycleTimes.StationEntry ) < 3
END
ELSE
BEGIN
SELECT
Testerline1_CycleTimes.Station,
Testerline1_CycleTimes.StationEntry
WHERE
Testerline1_CycleTimes.Station LIKE @Station
AND Testerline1_CycleTimes.StationEntry Between @Start And @End
END