tags:

views:

558

answers:

6

What where clause should be use to get records from the last 24 hours?

A: 
select ...
from ...
where YourDateColumn >= getdate()-1
Mladen Prajdic
A: 
select * from table_name where date_col > sysdate-1; //Oracle
select * from table_name where date_col > now()-1; //Mysql
DKSRathore
+6  A: 

In MySQL:

SELECT  *
FROM    mytable
WHERE   record_date >= SYSDATE() - 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
Quassnoi
A: 
SELECT * 
FROM tableName 
WHERE datecolumn >= dateadd(hour,-24,getdate())
Galwegian
+1  A: 

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.

Andrew
+2  A: 
SELECT * 
FROM table_name
WHERE table_name.the_date > DATE_SUB(CURDATE(), INTERVAL 1 DAY)
Guillaume Flandre
If the records are stored as `DATETIME`, this will select all records from the previous date, disregarding the time part. Been run at `23:59:59`, the query will return all records for the last `48` hours, not `24`.
Quassnoi