Skip to content
Advertisement

join tables time difference of 10 minutes [datetime2](7) NULL

I want to join the values of 2 tables date for specific minutes . say data join for same date but hours can be + or – 10 minutes in difference.

select * from table1 inner join table2 where table1.starttime = table2.starttime

Here, I have an wait time of 10 minutes and the values has to be shown in case of difference is + or – 10 minutes

Advertisement

Answer

Do you want something like this?

select *
from table1 t1 inner join 
     table2 t2
     on t1.starttime >= dateadd(minute, -10, t2.starttime) or
        t1.starttime <= dateadd(minute, 10, t2.starttime);

This doesn’t check for the same day. If that is also desirable:

select *
from table1 t1 inner join 
     table2 t2
     on ( t1.starttime >= dateadd(minute, -10, t2.starttime) or
          t1.starttime <= dateadd(minute, 10, t2.starttime)
        ) and
        convert(date, t1.starttime) = convert(date, t2.starttime);
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement