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
 SQL Server 2005 Forums
 Transact-SQL (2005)
 To get the last 6 hrs of data !

Author  Topic 

bondwiththebest
Starting Member

46 Posts

Posted - 2008-06-13 : 17:48:19
Hi ,

i would like to know if you can help me to find out on how to select only the last 6 hrs of data from the db.

i tired to get the last one day of data using

""select ....

from ....

where datetimecolumn >convert(varchar(10),getdate()-1,1)
""

but this process it takes a lot of time to get the data.

Can you please help me to find out how to get only the last 6hrs of data ??that would be really helpful for me.

thankyou

Bustaz Kool
Master Smack Fu Yak Hacker

1834 Posts

Posted - 2008-06-13 : 19:42:48
I assume that you want to get the last six hours of the data from a table; not the database.

SELECT ...
from ...
WHERE datetimecolumn >= DateAdd(hour, -6, GetDate())

Does that help?


=======================================
We should try to be the parents of our future rather than the offspring of our past. -Miguel de Unamuno
Go to Top of Page

bondwiththebest
Starting Member

46 Posts

Posted - 2008-06-16 : 08:37:50
that one works fine but i am not sure if the date is changed! Lets say at midnight, and if the run that query, it will get only that days data but not the last 6hrs of data ,right?
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2008-06-16 : 08:54:07
quote:
Originally posted by bondwiththebest

that one works fine but i am not sure if the date is changed! Lets say at midnight, and if the run that query, it will get only that days data but not the last 6hrs of data ,right?


You would get last 6 hours data based on current date and time

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

bondwiththebest
Starting Member

46 Posts

Posted - 2008-06-17 : 20:18:16
Here is my prob. I have to update for every 4 hrs and i am using a condition. To insert the data , i am selecting only the last 6hrs of data.

select ..

from ..

where datetime>=dateadd(hour,-6,getdate()) ( as suggested in previous code)

but then this doesnt work when it is at midnight.

I have two col. starttime and endtime where the endtime is updated at the end of the shift.So if the start is before date and endtime is at the next date. The getdate() function is not working.

Is there any better way of doing it??

thanks

Go to Top of Page

bondwiththebest
Starting Member

46 Posts

Posted - 2008-06-17 : 20:18:17
Here is my prob. I have to update for every 4 hrs and i am using a condition. To insert the data , i am selecting only the last 6hrs of data.

select ..

from ..

where datetime>=dateadd(hour,-6,getdate()) ( as suggested in previous code)

but then this doesnt work when it is at midnight.

I have two col. starttime and endtime where the endtime is updated at the end of the shift.So if the start is before date and endtime is at the next date. The getdate() function is not working.

Is there any better way of doing it??

thanks

Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-06-18 : 01:31:16
quote:
Originally posted by bondwiththebest

Here is my prob. I have to update for every 4 hrs and i am using a condition. To insert the data , i am selecting only the last 6hrs of data.

select ..

from ..

where datetime>=dateadd(hour,-6,getdate()) ( as suggested in previous code)

but then this doesnt work when it is at midnight.

I have two col. starttime and endtime where the endtime is updated at the end of the shift.So if the start is before date and endtime is at the next date. The getdate() function is not working.

Is there any better way of doing it??

thanks




DIdint get that. Can you elaborate? WHat are datatypes of start and endtime fields? what value will they be containing ?both date & time or date alone?
Go to Top of Page
   

- Advertisement -