SQLTeam.com | Weblogs | Forums

Find records between current date & time minus 1 hour

tsql

#1

Hello

I have a batch job which runs every top of the hour and i want to populate all the records in that time frame, please help me with below query.

CREATEDDATE BETWEEN '2016/12/13 08:00' AND '2016/12/13 09:00'
( considering job date & time is 9:00 so time difference is '2016/12/13 08:00' to '2016/12/13 09:00' )

thanks for help


#2

Try this:

WHERE
CREATEDDATE >= DATEADD(HOUR, DATEDIFF(HOUR, 0, GETDATE()) - 1, 0) AND
CREATEDDATE < DATEADD(HOUR, DATEDIFF(HOUR, 0, GETDATE()), 0)