MariaDB YEAR(), MONTH(), and DAY(): Extract date parts and filter a month

MariaDB YEAR(), MONTH(), and DAY() return the numeric year, month, and day of the month. DAY() is a synonym for DAYOFMONTH(). It does not return the weekday. For a month filter, you can use a range from the start of the month inclusive to the start of the next month exclusive. MariaDB Docs · YEAR() MariaDB Docs · MONTH() MariaDB Docs · DAYOFMONTH()

Extract year, month, and day as numbers

Run the first SELECT without a table. The leap-day example yields 2024, 2, 29, and 29. MONTH() returns the number 2 rather than the formatted text 02. If you need the full date, DATE() extracts the date part directly.

SELECT YEAR('2024-02-29') AS year_part,
       MONTH('2024-02-29 08:10:20') AS month_part,
       DAY('2024-02-29') AS day_part,
       DAYOFMONTH('2024-02-29') AS same_day;
Function Returns Example
YEAR(date) Year 2024
MONTH(date) Month 2
DAY(date), DAYOFMONTH(date) Day of month 29

Set up DATETIME values at month boundaries

Run the table examples in order using the same connection to a practice database called learning. You need create, insert, and select privileges. Use a separate practice database if the table name exists. DATETIME(6) preserves fractional seconds at the end of the month. All three results for ID 6 are NULL. MariaDB Docs · DATETIME

USE learning;
CREATE TABLE date_parts_demo (
    id INT PRIMARY KEY,
    created_at DATETIME(6) NULL,
    KEY idx_created_at (created_at)
);
INSERT INTO date_parts_demo (id, created_at) VALUES
    (1, '2024-01-31 23:59:59'),
    (2, '2024-02-01 00:00:00'),
    (3, '2024-02-29 23:59:59.999999'),
    (4, '2024-03-01 00:00:00'),
    (5, '2023-02-28 12:00:00'),
    (6, NULL);
SELECT id, created_at,
       YEAR(created_at) AS y,
       MONTH(created_at) AS m,
       DAY(created_at) AS d
FROM date_parts_demo ORDER BY id;

Distinguish every February from February 2024

SELECT id FROM date_parts_demo
WHERE MONTH(created_at) = 2
ORDER BY id;

SELECT id FROM date_parts_demo
WHERE YEAR(created_at) = 2024 AND MONTH(created_at) = 2
ORDER BY id;

SELECT id FROM date_parts_demo
WHERE created_at >= '2024-02-01'
  AND created_at < '2024-03-01'
ORDER BY id;

The first filter returns IDs 2, 3, and 5 because it accepts February in any year. The next two return IDs 2 and 3. The range includes February 29 at 23:59:59.999999 and excludes midnight on March 1. An upper bound set to midnight on the last day would lose its later times.

A function on an indexed date column does not rule out the index in all cases. From MariaDB 11.1, eligible YEAR() and DATE() comparisons can be rewritten as ranges. Do not assume that MONTH() gets the same rewrite. Check the actual plan with EXPLAIN. MariaDB Docs · Sargable DATE and YEAR

Interpret NULL and zero dates

SELECT YEAR(NULL) AS y, MONTH(NULL) AS m, DAY(NULL) AS d;
SELECT YEAR('0000-00-00') AS y,
       MONTH('0000-00-00') AS m,
       DAY('0000-00-00') AS d;

All three functions return NULL for a NULL input. When the server accepts '0000-00-00', extracting the parts gives 0, 0, and 0. It is not a valid calendar date. Warnings and errors depend on the type and SQL mode. Check what a zero date represents in the source data before replacing it. MariaDB Docs · SQL_MODE

Check the session time zone for TIMESTAMP

Run the final block in one connection. A TIMESTAMP entered as 2024-02-29 15:30:00 in +00:00 displays as 2024-03-01 00:30:00 in +09:00, so the parts become 2024, 3, and 1. The DATETIME shows the original calendar value 2024-02-29 15:30:00. MariaDB Docs · TIMESTAMP

SET @previous_time_zone = @@session.time_zone;
SET SESSION time_zone = '+00:00';
CREATE TABLE date_parts_zone_demo (
    ts TIMESTAMP NULL,
    dt DATETIME NULL
);
INSERT INTO date_parts_zone_demo (ts, dt) VALUES
    ('2024-02-29 15:30:00', '2024-02-29 15:30:00');
SELECT ts, YEAR(ts), MONTH(ts), DAY(ts),
       dt, YEAR(dt), MONTH(dt), DAY(dt)
FROM date_parts_zone_demo;
SET SESSION time_zone = '+09:00';
SELECT ts, YEAR(ts), MONTH(ts), DAY(ts),
       dt, YEAR(dt), MONTH(dt), DAY(dt)
FROM date_parts_zone_demo;
SET SESSION time_zone = @previous_time_zone;

Choose the reporting calendar’s time zone first and align the session zone and filter boundaries. A fixed offset may miss daylight saving changes in other regions. The example changes only the current connection and restores its previous setting.