Overlapping date ranges

  • If I have a table with a begin_date & end_date field, does anyone know a good method to check for overlapping date ranges?

    Thanks!

    Kurt 

  • Please post DDL and sample data for this table. Are you searching an entire table for overlapping dates? Or a subset based on a key? Or...?

    --
    Adam Machanic
    whoisactive

  • Select * From [Table] Where Begin_Date Between '01/01/2004' And '01/15/2004'??  Not sure exactly what you're looking for

  • Try:

    select * from tablename a inner join tablename b on a.RecID <> b.RecID and ((a.begin_date <= b.end_date and a.end_date >= b.end_date) or (a.end_date >= b.begin_date and a.begin_date <= b.begin_date))

    assuming that 'tablename' is your table and 'RecID' is the record id field

    Regards

    Peter

     

  • declare @DateRanges table ( BeginDt datetime, EndDt datetime )

    INSERT INTO @DateRanges

          SELECT Begin_Date, End_Date FROM MyTable

     

    SELECT A.* FROM MyTable A

          WHERE 1 < ( SELECT COUNT(*) FROM @DateRanges B

                                  B.BeginDt <= A.End_Date AND B.EndDt >= A.Begin_Date )

    The above can be done without the temp table but will likely not perform as well. I haven't tried running it so the bottom most where clause might not quite be correct, but I think that this should at least be the general idea of what you are after.

  • thanks!

Viewing 6 posts - 1 through 6 (of 6 total)

You must be logged in to reply to this topic. Login to reply