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.
| 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 atemptSELECT @Count = COUNT(*)FROM ShoppingCartWHERE (ShoppingCart.DateAdded < DATEADD(day, DATEDIFF(day, 0, GETDATE()) - 2, 0)) AND (pSetID = @pSetID).DaveHelixpoint Web Developmenthttp://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. |
 |
|
|
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 ShoppingCartWHERE datediff(mi,ShoppingCart.DateAdded ,getdate()) <= 15 AND (pSetID = @pSetID). |
 |
|
|
theboyholty
Posting Yak Master
226 Posts |
Posted - 2009-04-16 : 11:59:53
|
Its up to you which of these two options you use |
 |
|
|
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. |
 |
|
|
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 usewhere ShoppingCart.DateAdded < dateadd(mi,-15,getdate())... |
 |
|
|
theboyholty
Posting Yak Master
226 Posts |
Posted - 2009-04-17 : 04:30:27
|
| why's that then? |
 |
|
|
|
|
|