check Record (Slot) is already exists between Start and End time in SQL Server

 In this  article I will show you how to  check Record is already exists between Start and End time .
I have a table ,its having start and end time column with 3 rows(or it may be n of rows).so i am putting here my SQL query to get those records if any record  have time between Start and end time .

DECLARE @mytable as Table
(
    StartTime time,
    EndTime time
)

INSERT INTO @mytable VALUES
('08:00', '08:30'),
('10:30', '12:30'),
('12:30', '14:15')

DECLARE @StartTime time, @EndTime time

SELECT  @StartTime = '14:15',
        @EndTime = '18:15'

  SELECT *
FROM @mytable
WHERE
(
    StartTime < @EndTime
    AND EndTime > CASE WHEN @StartTime > @EndTime THEN '00:00' ELSE @StartTime END
)
OR
(

    @StartTime > @EndTime
    AND StartTime > EndTime
)
OR
(
    @StartTime < @EndTime
    AND StartTime > EndTime
    AND EndTime > @StartTime      
)


Note : I have taken is considertion that the time peroid might inlcude two diffrent days.you can filter it by adding new paramter as current selected date

Comments

Popular posts from this blog

Bootstrap Modal Popup keep open on PostBack in ASP.Net

Resolved Issue in Asp core 3.0 serializersettings does not exist in AddJsonOptions

.Net most asked interview questions for experienced professionals (C#,Asp WEBFORM,MVC,ASP CORE,WEB API,SQL Server,Java Script,Jquery)