in sql server the date+time (aka now) is available from GETDATE()
Main Topics
Browse All TopicsI have stored procedure which runs wvery hour. However every hour I need to dynam,ixally change date/time,. I need two cases
a. Run query for previous day to concompass the whole day
select * from Table where dateTime>=(sysdate-1):0000
b. I need a query which looks for anything for the past one hour for today
select * from Table where dateTime>=sysdate-1:curren
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
If you happen to be using SQL Server 2008, you can make use of the new Date datatype to accept the dateadd(day, -1, getdate()) value from angelIII's solution for the "yesterday" portion of the problem and then match based upon your dates being between that value and that value + 1.
If you are not on SS2008, you can make use of the fact that the dates are integers and the times are fractions in a double datatype. (See SQL_1 below)
If you want the process to check for exactly and precisely datetimes during the hour immediately preceding the execution, angelIII's solution is your answer for the second half of the problem.
angellll
select * from Table
where dateTime>=dateadd(day, -1, getdate())
and dateTime<=dateadd(hour,-1, getdate())
does not giove me all records from last one day ie
I need records from
select * from Table
and dateTime > '04/12/09 00:00'
and dateTime < '04/13/09 09:00' (assuming 09:00 is the current time)
How can I add the 00:00 to the above query
Business Accounts
Answer for Membership
by: chapmandewPosted on 2009-04-10 at 13:51:41ID: 24119077
a.
select * from tablename
where datefield >= cast(convert(varchar(10), dateadd(d, -1, getdate(), 101) as datetime)
and datefield < cast(convert(varchar(10), getdate(), 101) as datetime)
b.
select * from tablename
where datefield >= cast(convert(varchar(10), dateadd(h, -1, getdate(), 101) as datetime)