Shift 3 passes in 2 days.
Add these terms also with your request.
AND CONVERT(Time,DATEADD(DD,1,RegisteredDateTime)) < '07:00:00' AND CONVERT(Time,RegisteredDateTime) > '07:00:00' AND DATEDIFF(DD,CONVERT(DATE,RegisteredDateTime), CONVERT(Date,DATEADD(DD,1,RegisteredDateTime))) <=1
Strike>
This answer is based on Richard aka cyberkiwi's answer above. Full credit goes to him. Please accept his answer if this works for you.
your shift data is as follows:
Shift1 : 07:00:00-12:00:00 Shift2 : 12:00:00-22:00:00 Shift3 : 22:00:00-07:00:00
you just need to set @shift_start_time and @shift_end_time based on switch time
declare @shift_start_time time ; declare @shift_end_time time; IF shift 1: select @shift_start_time ='00:00:00' select @shift_end_time ='04:59:59' IF shift 2: select @shift_start_time ='05:00:00' select @shift_end_time ='14:59:59' IF shift 3: select @shift_start_time ='15:00:00' select @shift_end_time ='23:59:59' SELECT RegisteredDateTime FROM t_shift WHERE CONVERT(Time, DateAdd(hh,-7, RegisteredDateTime)) between @shift_start_time and @shift_end_time
source share