Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Use Date/Time Function to return last 6 hours

Author  Topic 

Almir_7
Starting Member

14 Posts

Posted - 2008-11-14 : 17:43:40
I'm trying to convert/change my select query to return only last 6 hours of data instead of last two days.

Here is that i have now. I use these two variables to select data that is writen between these two dates:

declare @StartDate datetime
set @StartDate = Convert(VarChar, GetDate() - 1, 101)

declare @EndDate datetime
set @EndDate = Convert(VarChar, GetDate(), 101)

Now i want to return only last 6 hours at the time. Is something like this possible?

set @StartDate = CONVERT(varchar, GetDate() - 0/0/0000 06:00, 113) 'dd Mmm yyyy hh:mm'
set @EndDate = CONVERT(varchar, GetDate(), 113) 'dd Mmm yyyy hh:mm'

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-11-14 : 23:26:49
Dont change dates to varchars. Do mainpulation keeping them as dates itself. for getting last 6 hours data just use

WHERE yourdatefield> DATEADD(hh,-6,GETDATE())
Go to Top of Page
   

- Advertisement -