To extract only the month and year from a DATE, DATETIME, DATETIME2, or DATETIMEOFFSET column in SQL, use the functions provided by your SQL dialect—most commonly YEAR() + MONTH() or FORMAT() in SQL Server (T-SQL), and YEAR() + MONTH() or DATE_FORMAT() in MySQL. For grouping, filtering, or sorting, return numeric/date components rather than formatted strings whenever possible so chronological ordering and index usage remain predictable.
- SQL Server (T-SQL): Numeric Parts vs. Formatted Output Numeric Parts (YEAR() and MONTH()) — Best for GROUP BY and Ordering Returning YEAR(col) and MONTH(col) as integers keeps values numeric, avoids locale/string sorting bugs (‘10-2026’ vs. ‘2-2026’), and is straightforward to group by:
SQL SELECT YEAR(created_at) AS order_year, MONTH(created_at) AS order_month, COUNT(*) AS total_orders, SUM(total_amount) AS monthly_sales FROM dbo.orders GROUP BY YEAR(created_at), MONTH(created_at) ORDER BY order_year DESC, order_month DESC; Single YYYY-MM Value via FORMAT() or CONVERT() If you need a single ‘YYYY-MM’ label for display or reporting:
SQL – Using FORMAT() (flexible, though higher CPU overhead on very large rowsets) SELECT order_id, created_at, FORMAT(created_at, ‘yyyy-MM’) AS year_month_label FROM dbo.orders;
– Fast string slicing from ISO style 23 (YYYY-MM-DDTHH:MI:SS) for DATETIME/DATETIME2/DATE SELECT order_id, created_at, LEFT(CONVERT(VARCHAR(30), created_at, 126), 7) AS year_month_iso FROM dbo.orders; Note: In FORMAT(), lowercase mm means minutes, whereas uppercase MM means month.
- MySQL: YEAR(), MONTH(), and DATE_FORMAT() In MySQL, use YEAR() and MONTH() for numeric components, or DATE_FORMAT(col, ‘%Y-%m’) for formatted output:
SQL – Numeric year and month for grouping/sorting SELECT YEAR(created_at) AS order_year, MONTH(created_at) AS order_month, COUNT(*) AS total_orders FROM orders GROUP BY order_year, order_month ORDER BY order_year DESC, order_month DESC;
– Formatted ‘YYYY-MM’ string SELECT order_id, DATE_FORMAT(created_at, ‘%Y-%m’) AS year_month_str FROM orders; In MySQL DATE_FORMAT(), %Y is the 4-digit year, %m is the 2-digit month (01–12), and %M is the full month name.
- Filtering by Month and Year Without Breaking Index Seeks Wrapping the column in YEAR(created_at) or MONTH(created_at) inside a WHERE clause is non-SARGable—SQL Server or MySQL generally cannot perform a clean range seek on a bare index over created_at. Instead, filter with a half-open date range using ISO literals (YYYY-MM-DD):
SQL – Avoid (wraps column in function in WHERE): SELECT order_id, created_at FROM dbo.orders WHERE YEAR(created_at) = 2026 AND MONTH(created_at) = 11;
– Prefer (index-friendly range predicate for November 2026): SELECT order_id, created_at FROM dbo.orders WHERE created_at >= ‘2026-11-01T00:00:00’ AND created_at < ‘2026-12-01T00:00:00’; 4. Operational Checklist Use numeric YEAR() and MONTH() (or DATE_FORMAT(…, ‘%Y-%m’)) for sorting and grouping—avoid sorting formatted month names alphabetically (April, August, December is not chronological).
Double-check case sensitivity in format strings: MM is month in SQL Server FORMAT(), while mm is minute.
Use half-open date ranges (>= start AND < next_month_start) in WHERE rather than YEAR(col) = … AND MONTH(col) = … on large indexed tables.
Keep formatting in the presentation/reporting layer when you only need a display label from a typed DATE or DATETIME2 column.
