SQL Server 2008 - Compare time column to current DateTime

3.1k views Asked by At

I have read through related posts but have not yet found an answer so if I may:

I have a column created in my table as follows:

[Arrival] time NOT NULL,

What is the most efficient way to perform a query like this:

select * from myTable where "Arrival is 1 hour from now" ?

DATEDIFF(hour, GETDATE(), Arrival)

gives me a very large value, possible because of the 1901,0,0 date part of Arrival. Still trying to understand this so if anyone has a quick answer for me, that will be great.

Again, performance is critical.

Thanks.

1

There are 1 answers

1
swasheck On BEST ANSWER

I'd probably do something like ...

select >>select list<<
from table
where datediff(minute, getdate(), [Arrival]) < 60

or, using your hour

select >>select list<<
from table
where datediff(hour, getdate(), [Arrival]) < 1

edit: Also, if performance is critical then I would also suggest validating input to not allow ancient dates. If you have recurring schedules that are not dependent on year, or even month (essentially, you're just looking for hours and minutes) then some usage of DatePart would be appropriate.

select ____
from [table]
where (((datepart(hour, getdate()) = datepart(hour, [Arrival])) AND (datepart(minute, [Arrival]) - datepart(minute, getdate()) < 60)