Wednesday, February 27, 2013

 

Dates Overlapping logic: 

DECLARE
@d1 DATETIME = '02-10-2012'

DECLARE
@d2 DATETIME = '02-14-2013'

IF
NOT EXISTS (SELECT 1 FROM table1

WHERE (@d1 BETWEEN start_datetime AND end_datetime) OR


(@d2 BETWEEN start_datetime AND end_datetime) OR


(start_datetime BETWEEN @d1 AND @d2) OR


(end_datetime BETWEEN @d1 AND @d2))

BEGIN

INSERT INTO Table1 (start_datetime,end_datetime,Comments)

VALUES (@D1, @d2 , 'Success')

END

SELECT
* FROM Table1