// SQL snippet

Validate impossible dates

Combine range checks and ordering rules to find date values that cannot be true.

Main statement
PostgreSQL
Couvre également
SQL Server SQLite

The task

Date bugs arrive as future order dates, 1970-style zero values from broken imports, and shipments timestamped before the order itself. Each is a small predicate away from detection.

The snippet

Written for PostgreSQL. Versions for SQL Server and SQLite are under Variations.

PostgreSQL
SELECT id, order_date, shipped_date, status
FROM orders
WHERE order_date > CURRENT_DATE
   OR order_date < DATE '1900-01-01'
   OR shipped_date < order_date;

Fonctionnement

Three predicates cover the classic defects: dates in the future, dates before the system could plausibly have data, and logical impossibilities like shipped_date preceding order_date. Each row returned breaks at least one rule; the first variant labels which. DATE '1900-01-01' is the standard date literal, which MySQL and Oracle also accept. SQL Server and SQLite reject it, so the second variant uses a plain string there. Do not reach for CAST('1900-01-01' AS DATE) to stay portable: Oracle parses it with NLS_DATE_FORMAT and fails, and SQLite turns it into the integer 1900, so the comparison is silently never true. If your bad imports used 1970-01-01 or 0001-01-01 as a placeholder, raise the floor to whatever date your data cannot predate.

Variations

Label the defect per row

SELECT o.id,
       CASE WHEN o.order_date > CURRENT_DATE THEN 'future order date' END AS defect_a,
       CASE WHEN o.order_date < DATE '1900-01-01' THEN 'implausibly old date' END AS defect_b,
       CASE WHEN o.shipped_date < o.order_date THEN 'shipped before ordered' END AS defect_c
FROM orders o
WHERE o.order_date > CURRENT_DATE
   OR o.order_date < DATE '1900-01-01'
   OR o.shipped_date < o.order_date;

SQL Server and SQLite: string literal, NULL counted as a defect

SQL Server SQLite
SELECT id, order_date, shipped_date
FROM orders
WHERE order_date IS NULL
   OR order_date > CURRENT_DATE  -- SQL Server before 2025: CAST(GETDATE() AS date)
   OR order_date < '1900-01-01'
   OR shipped_date < order_date;

Questions that come up

Does a DATE column even need range validation?

The type rules out 'February 30th' on most engines, but not business impossibilities: future events, placeholder dates from bad imports and reversed orderings all fit in a legal DATE. SQLite has no date type at all, so there a column can even hold '2026-02-30' or 'n/a'; add date(order_date) IS NOT order_date to the WHERE, which catches both unparseable text and impossible days (SQLite rolls '2026-02-30' over to 2026-03-02). Once the table is clean, the floor and ordering rules can become CHECK constraints; the future-date rule cannot everywhere, because Oracle rejects CURRENT_DATE in a CHECK (ORA-02436).

Keep reading

// snippets are better with a schema brain

Run these with autocomplete that knows your tables

SQLly is a native SQL workbench for macOS, Windows and Linux. Paste a snippet into a live connection and you get schema-aware IntelliSense, a plan viewer and guards against unsafe edits.