This query will work with your current table construction as long as the time data is in the correct long format - i.e. with preceding zeroes on values less than 10.
DECLARE @testTime TIME = CAST('02:58 PM' AS TIME)
SELECT [id],[CollectionTimeSlot],
CAST(SUBSTRING([CollectionTimeSlot],1,8) AS TIME),
CAST(SUBSTRING([CollectionTimeSlot],13,8) AS TIME)
FROM tbl_CollectionTimeSlot
WHERE @testTime BETWEEN CAST(SUBSTRING([CollectionTimeSlot],1,8) AS TIME) AND CAST(SUBSTRING([CollectionTimeSlot],13,8) AS TIME)
The BETWEEN on the WHERE statement is exactly the same as saying
WHERE @testTime >= CAST(SUBSTRING([CollectionTimeSlot],1,8) AS TIME) AND @testTime <= CAST(SUBSTRING([CollectionTimeSlot],13,8) AS TIME)
However, that is an awful table schema!
Always use the correct data type for your table columns. Varchar (or char, or nvarchar or nchar) are
not appropriate data types for TIME data.
This would have been better:
create table CollectionTimeSlot
(
id int identity(1,1),
startTime TIME,
endTime TIME
)
insert into CollectionTimeSlot values
('12:00 AM','02:00 AM'),
('02:00 AM','04:00 AM'),
('04:00 AM','06:00 AM'),
('06:00 AM','08:00 AM'),
('08:00 AM','10:00 AM'),
('10:00 AM','12:00 PM'),
('12:00 PM','02:00 PM'),
('02:00 PM','04:00 PM'),
('04:00 PM','06:00 PM'),
('06:00 PM','08:00 PM'),
('08:00 PM','10:00 PM'),
('10:00 PM','12:00 AM')
Then querying the data becomes trivial
DECLARE @testTime2 TIME = CAST('02:58 PM' AS TIME)
SELECT * FROM CollectionTimeSlot
WHERE @testTime2 BETWEEN startTime AND endTime