How can I force TSQL to accept two dates EQUAL if there is only an n miliseconds difference?
-
27-03-2021 - |
Pergunta
I have two tables, table1 and table2.
And I join them by comparing a couple of columns and one of them is a Date column.
select *
from table1 t1
INNER JOIN table2 t2 ON t1.HaloDate = t2.HaloDate
My question is I found that the join fails for comparison of 2 dates below:
'2011-07-23 14:01:32.113' and '2011-07-23 14:01:32.114'
is it possible to write this join by ignoring the miliseconds or by introducing a range like 5 milisecons as a tolerance range?
Thanks
Solução
Yes:
select *
from table1 t1
INNER JOIN table2 t2
ON t1.HaloDate between dateadd(ms,-5,t2.HaloDate) and dateadd(ms,5,t2.HaloDate)
Outras dicas
Use DATEDIFF function and place into it any range you want, for example:
ABS(DATEDIFF(second, DateField1, DateField2)) < 5
means that date values difference less than 5 seconds.
You can use any tag for time range - from milliseconds to years
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow