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 2000 Forums
 Transact-SQL (2000)
 How To Query Tim Now - 4 Hours

Author  Topic 

sylvaink
Starting Member

2 Posts

Posted - 2010-10-29 : 02:23:56
Hi,

I need to query a table and I need the result from all records where the datetime field is older then 4 hours.

Somebody a Tip for me ?

pk_bohra
Master Smack Fu Yak Hacker

1182 Posts

Posted - 2010-10-29 : 06:51:55
Select * from yourtable where datetimeField < dateadd datediff(hh,-4,getdate())
Go to Top of Page

nr
SQLTeam MVY

12543 Posts

Posted - 2010-10-29 : 06:53:29
or use dateadd :)
Select * from yourtable where datetimeField < dateadd(hh,-4,getdate())

==========================================
Cursors are useful if you don't know sql.
DTS can be used in a similar way.
Beer is not cold and it isn't fizzy.
Go to Top of Page

Kristen
Test

22859 Posts

Posted - 2010-10-29 : 08:02:42
Personally I prefer to use the word "Hour" for the first parameter.

Things like Month, Minute, Millisecond, (and now Microsecond in SQL2008) are way too difficult for me to remember the "M", "MM", ... abbreviations, or to assume that other people reading my code will know them reliably!)
Go to Top of Page

sylvaink
Starting Member

2 Posts

Posted - 2010-10-29 : 08:36:15
Hi,


Datediff didn't do what I need still get result before the 4 hours.
< DATEDADD(hh; - 4; GETDATE()) This is working and did the thing for me.
Also < GETDATE () -0,xx is working a little bit more difficult to implement because you don't now how much time 0,10 is for example.

Thanks to everybody for the reply's.
Go to Top of Page

pk_bohra
Master Smack Fu Yak Hacker

1182 Posts

Posted - 2010-10-29 : 08:45:24
OOPS my mistake.
I passed the parameters correctly but mistyped dateadd as datediff.
Go to Top of Page
   

- Advertisement -