SQLPathLab
Date and Time · Basic

DATE_FORMAT

Formats a date or datetime value as a specific string.

Overview

DATE_FORMAT is a MySQL function that returns text according to a format string. This example uses %Y-%m for reporting output. Avoid wrapping an indexed date column in DATE_FORMAT for large-table filters; use a date range instead. Other databases use different functions and format tokens.

Basic syntax

SELECT DATE_FORMAT(created_at, '%Y-%m') AS month FROM orders;

Examples

DATE_FORMAT example

Format each order created_at value as YYYY-MM and observe the returned month text.

Sample table

Table:orders
orders
created_at
2026-01-03
2026-01-08
2026-02-02
2026-02-15

Run result

result
month
2026-01
2026-01
2026-02
2026-02

Common issues

Should DATE_FORMAT be used in WHERE filters?

Avoid wrapping indexed date columns in DATE_FORMAT in large-table filters because it may hurt index usage.

Does DATE_FORMAT return a date or a string?

It usually returns a formatted string.

Is DATE_FORMAT required for monthly aggregation?

Not always. YEAR/MONTH or date ranges may be better depending on display and performance needs.

Tags

CommonDate & time

Was this explanation clear?