LLI Wiki — My Digital Knowledge Base

Filtering the last 24 hours across SQL engines

Equivalent date conditions for the last 24 hours in MySQL, SQL Server, Oracle, PostgreSQL, Redshift, SQLite and MS Access.

The following filter selects the rows whose date falls within the last 24 hours. The date expression differs between SQL engines.

-- MySQL
SELECT  *
FROM    mytable
WHERE   record_date >= NOW() - INTERVAL 1 DAY

-- SQL Server
SELECT  *
FROM    mytable
WHERE   record_date >= DATEADD(day, -1, GETDATE())

-- Oracle
SELECT  *
FROM    mytable
WHERE   record_date >= SYSDATE - 1

-- PostgreSQL
SELECT  *
FROM    mytable
WHERE   record_date >= NOW() - '1 day'::INTERVAL

-- Redshift
SELECT  *
FROM    mytable
WHERE   record_date >= GETDATE() - '1 day'::INTERVAL

-- SQLite
SELECT  *
FROM    mytable
WHERE   record_date >= datetime('now','-1 day')

-- MS Access
SELECT  *
FROM    mytable
WHERE   record_date >= (Now - 1)
Mastodon LLIMastodon SocialFosstodon