I am trying to get certain Dates that fit in timeslots from a table where
- the timeslot starts at the current DateTime floored to full hour
- the timeslot ends at the current DateTime floored to full hour plus 2 hours
so a DateTime that is 2011-06-24 09:21:40.020 could be between:
2011-06-24 09:00:00.000 AND 2011-06-24 11:00:00.000
Currently I’ve got this, but I think the nested DATEADD is a redundant. Is there a cleaner way to do this?
SELECT dbo.Computer.ComputerName, dbo.Planned.DatePlanned
FROM dbo.Computer INNER JOIN
dbo.Planned ON dbo.Computer.ComputerID = dbo.Planned.ComputerID
WHERE dbo.Planned.DatePlanned
BETWEEN
(SELECT DATEADD(Hour, DATEDIFF(Hour, 0, GETDATE()), 0))
AND
(SELECT DATEADD(Hour, DATEDIFF(Hour, 0, DATEADD(Hour, 2, GETDATE())), 0))
This should give me all ComputerNames and Planned Dates in the current active timeslot.
I think it is cleaner to define those two variables: