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)
 Subtracting time

Author  Topic 

helixpoint
Constraint Violating Yak Guru

291 Posts

Posted - 2009-04-16 : 11:44:57
I have a table that has records in it from a shopping cart that contain a a productID and a date. I want to do a select statement that checks to see if and of the records in that table are less than 15 minutes old. Then I send back a count if they are. This is my atempt

SELECT @Count = COUNT(*)
FROM ShoppingCart
WHERE (ShoppingCart.DateAdded < DATEADD(day, DATEDIFF(day, 0, GETDATE()) - 2, 0)) AND (pSetID = @pSetID).


Dave
Helixpoint Web Development
http://www.helixpoint.com

theboyholty
Posting Yak Master

226 Posts

Posted - 2009-04-16 : 11:56:54
Use this:

WHERE DATEDIFF(mi,ShoppingCart.DateAdded,GETDATE()) < 15


Your query was testing the date on the basis of the day, the line above compares the DateAdded to GETDATE() on the basis of the minutes.
Go to Top of Page

vijayisonly
Master Smack Fu Yak Hacker

1836 Posts

Posted - 2009-04-16 : 11:57:57
quote:
less than 15 minutes old.


may be this?

SELECT @Count = COUNT(*)
FROM ShoppingCart
WHERE datediff(mi,ShoppingCart.DateAdded ,getdate()) <= 15 AND (pSetID = @pSetID).
Go to Top of Page

theboyholty
Posting Yak Master

226 Posts

Posted - 2009-04-16 : 11:59:53
Its up to you which of these two options you use
Go to Top of Page

vijayisonly
Master Smack Fu Yak Hacker

1836 Posts

Posted - 2009-04-16 : 12:02:24
I was a minute too late...hell with my typing.
Go to Top of Page

pdreyer
Starting Member

6 Posts

Posted - 2009-04-17 : 02:13:43
Don't use functions on columns else an index on that column can't be used.
Rather use
where ShoppingCart.DateAdded < dateadd(mi,-15,getdate())
...
Go to Top of Page

theboyholty
Posting Yak Master

226 Posts

Posted - 2009-04-17 : 04:30:27
why's that then?
Go to Top of Page
   

- Advertisement -