I am looking for a where
clause that can be used to retrieve records for the last 24 hours?
In Oracle (For last 24 hours):
SELECT *
FROM my_table
WHERE date_column >= SYSDATE - 24/24
In case, for any reason, you have rows with future dates, you can use between, like this:
SELECT *
FROM my_table
WHERE date_column BETWEEN (SYSDATE - 24/24) AND SYSDATE
in postgres, assuming your field type is a timestamp:
select * from table where date_field > (now() - interval '24 hour');
Hello i now it past a lot of time from the original post but i got a similar problem and i want to share.
I got a datetime field with this format YYYY-MM-DD hh:mm:ss, and i want to access a whole day, so here is my solution.
The function DATE(), in MySQL: Extract the date part of a date or datetime expression.
SELECT * FROM `your_table` WHERE DATE(`your_datatime_field`)='2017-10-09'
with this i get all the row register in this day.
I hope its help anyone.
In MySQL
:
SELECT *
FROM mytable
WHERE record_date >= NOW() - INTERVAL 1 DAY
In SQL Server
:
SELECT *
FROM mytable
WHERE record_date >= DATEADD(day, -1, GETDATE())
In Oracle
:
SELECT *
FROM mytable
WHERE record_date >= SYSDATE - 1
In PostgreSQL
:
SELECT *
FROM mytable
WHERE record_date >= NOW() - '1 day'::INTERVAL
In Redshift
:
SELECT *
FROM mytable
WHERE record_date >= GETDATE() - '1 day'::INTERVAL
In SQLite
:
SELECT *
FROM mytable
WHERE record_date >= datetime('now','-1 day')
In MS Access
:
SELECT *
FROM mytable
WHERE record_date >= (Now - 1)
Which SQL was not specified, SQL 2005 / 2008
SELECT yourfields from yourTable WHERE yourfieldWithDate > dateadd(dd,-1,getdate())
If you are on the 2008 increased accuracy date types, then use the new sysdatetime() function instead, equally if using UTC times internally swap to the UTC calls.