I have a smalldatetime
field named myTime
recording when the record was created. I need the syntax that selects records created within the last hour.>
If you want whole hours use the following...
--This Hour
SELECT *
FROM Whatever
WHERE myTime >= dateadd(hour, datediff(hour, 0, GETDATE()), 0)
--Last Hour
SELECT *
FROM Whatever
WHERE myTime < dateadd(hour, datediff(hour, 0, GETDATE()), 0) AND myTime >= dateadd(hour, datediff(hour, 0, DATEADD(HOUR, -1, GETDATE())), 0)
--Hour before last
SELECT *
FROM Whatever
WHERE myTime < dateadd(hour, datediff(hour, 0, DATEADD(HOUR, -1, GETDATE())), 0) AND myTime >= dateadd(hour, datediff(hour, 0, DATEADD(HOUR, -2, GETDATE())), 0)