Domanda

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

È stato utile?

Soluzione

Yes:

select *
from table1 t1 
INNER JOIN table2 t2 
ON t1.HaloDate between dateadd(ms,-5,t2.HaloDate) and dateadd(ms,5,t2.HaloDate) 

Altri suggerimenti

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

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top