Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL get within 24 hours [duplicate]

Possible Duplicate:
SQL Select within 24 hours?

I am using regular SQL to attempt to run a query. Here is what my table looks like:

Products
-----------------
Product ID  
Date 
Weight

The Date is enter as a datetime into the database.

I want to be able to select only the items whos Product.Date is within the past 24 hours. I have been trying to get this for sometime now and here is what I have gotten:

Select Products.Date
FROM Products
WHERE Products.Date < DATEADD(d,-1,CURRENT_TIMESTAMP)

This isn't working for some reason and I get the following product dates:

2011-01-18 00:00:00.000
2010-01-20 14:23:00.000
2011-01-20 04:05:00.000
like image 870
shenn Avatar asked May 21 '26 09:05

shenn


2 Answers

Could it be that you actually wanted > instead of <?

like image 155
Michael Krelin - hacker Avatar answered May 23 '26 00:05

Michael Krelin - hacker


As per my comment on the other post, if you only want the previous 24 hours (so excluding future dates) then try this:

SELECT Products.Date
FROM   Products
WHERE  Products.Date BETWEEN DATEADD(d,-1,CURRENT_TIMESTAMP) AND CURRENT_TIMESTAMP
like image 30
Stuart Blackler Avatar answered May 22 '26 23:05

Stuart Blackler



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!