SQL Keywords

SQL DATES

What does SQL DATES mean in database queries?

SQL DATES refers to storing, comparing, and manipulating calendar dates with the DATE data type, literals, and related functions.
Sign up to get up to date news on SQL keywords
Welcome to the Galaxy, Guardian!
You'll be receiving a confirmation email

Follow us on twitter :)
Oops! Something went wrong while submitting the form.

Compatible dialects for SQL DATES: PostgreSQL, MySQL, MariaDB, SQL Server (as DATE), Oracle, SQLite, Snowflake, BigQuery

SQL DATES Full Explanation

SQL DATES is a catch-all term that covers the DATE data type, date literals, and built-in operators that work exclusively with calendar dates (year-month-day) without a time-of-day component. A DATE column is stored internally as a three-part value (year, month, day). Most dialects follow the Gregorian calendar starting from 0001-01-01, but minimum and maximum ranges differ.Key behaviors- Precision: DAY is the smallest unit. Time and timezone information are never stored.- Comparisons: DATE values compare chronologically using standard operators (=, <, >, BETWEEN).- Arithmetic: Adding or subtracting integer days or INTERVALs shifts the date while respecting leap years.- Casting and literals: ISO-8601 format (YYYY-MM-DD) is portable. Standard SQL literal syntax is DATE '2024-06-14'.- Functions: CURRENT_DATE, DATE_ADD, DATE_SUB, EXTRACT(YEAR FROM date_val), and TRUNC are commonly supported.Caveats- Implicit time: Casting a DATE to TIMESTAMP usually sets the time portion to midnight (00:00:00).- Timezone shifts: Converting a TIMESTAMP to DATE first converts to session timezone, then truncates the time.- Range limits: MySQL supports 1000-01-01 to 9999-12-31, PostgreSQL -4713-01-01 to 5874897-12-31.- Non-deterministic functions: CURRENT_DATE depends on the session timezone and clock.

SQL DATES Syntax

-- Column definition
CREATE TABLE table_name (
    start_date DATE,
    end_date   DATE
);

-- Date literal
DATE '2024-06-14';

-- Cast string to DATE
CAST('2024-06-14' AS DATE);

-- Add 7 days
start_date + 7;

-- Extract year
EXTRACT(YEAR FROM start_date);

SQL DATES Parameters

Example Queries Using SQL DATES

-- 1. Create a table with a DATE column
CREATE TABLE events (
    id INT PRIMARY KEY,
    event_date DATE NOT NULL
);

-- 2. Insert a record for today
INSERT INTO events (id, event_date)
VALUES (1, CURRENT_DATE);

-- 3. Find events in the last 30 days
SELECT *
FROM events
WHERE event_date >= CURRENT_DATE - 30;

-- 4. Get month and day for birthdays
SELECT id,
       EXTRACT(MONTH FROM event_date)  AS birth_month,
       EXTRACT(DAY   FROM event_date)  AS birth_day
FROM events;

-- 5. Move all events one week later
UPDATE events
SET event_date = event_date + INTERVAL '7' DAY;

Expected Output Using SQL DATES

  • Table is created with one DATE column.
  • One row inserted with today's calendar date.
  • Query returns rows whose event_date is within the last 30 days.
  • Result shows numeric month and day columns.
  • All event_date values advance by exactly seven days.

Use Cases with SQL DATES

  • Modeling calendar-based data such as birthdays, holidays, due dates, and cut-off periods.
  • Filtering rows by a specific date range (e.g., last 30 days).
  • Grouping or reporting metrics by day.
  • Joining tables on shared calendar dates.
  • Performing schedule calculations like adding business days or computing age.

Common Mistakes with SQL DATES

  • Using DATETIME or TIMESTAMP when only a calendar date is needed, causing unwanted time components.
  • Forgetting to quote ISO literals: DATE 2024-06-14 (missing quotes) throws a syntax error.
  • Assuming CURRENT_DATE is timezone-agnostic; it depends on session settings.
  • Mixing DATE with VARCHAR strings, leading to implicit and non-index-friendly casts.
  • Adding INTERVAL '1 month' to January 31 without understanding end-of-month rules in each dialect.

Related Topics

DATE data type, DATETIME, TIMESTAMP, INTERVAL, CURRENT_DATE, CAST, EXTRACT

First Introduced In

SQL:1992 standard added the DATE data type

Frequently Asked Questions

What is the maximum range for the DATE data type?

Ranges vary by dialect: PostgreSQL supports dates to year 5874897, MySQL to 9999, SQL Server to 9999, SQLite to 5352 when stored as Julian day numbers.

How can I convert a string like '14/06/2024' to DATE?

Use an explicit parse or cast with a format mask: in PostgreSQL, `TO_DATE('14/06/2024', 'DD/MM/YYYY')`; in Oracle, `TO_DATE('14/06/2024','DD/MM/YYYY')`.

How do I calculate age from a birthday column?

PostgreSQL: `AGE(CURRENT_DATE, birthday)`.MySQL: `TIMESTAMPDIFF(YEAR, birthday, CURDATE())`.SQL Server: `DATEDIFF(year, birthday, GETDATE()) - CASE WHEN FORMAT(birthday,'MMdd') > FORMAT(GETDATE(),'MMdd') THEN 1 ELSE 0 END`.

Can a DATE column be indexed?

Yes. DATE columns are indexable in all major databases and often used in range queries for partition pruning and efficient lookups.

Sign up to get up to date news on SQL keywords
Welcome to the Galaxy, Guardian!
You'll be receiving a confirmation email

Follow us on twitter :)
Oops! Something went wrong while submitting the form.
Trusted by top engineers on high-velocity teams
Aryeo Logo
Assort Health
Curri
Rubie Logo
Bauhealth Logo
Truvideo Logo

Check out other commonly used SQL Keywords!