Sql Query To Get Month And Year From Date

4 min read

Introduction

When you need to sql query to get month and year from date, the solution varies slightly depending on the database system you are using. This article provides a clear, step‑by‑step guide that works across the most common SQL dialects—MySQL, PostgreSQL, SQL Server, and Oracle. By the end, you will have a solid understanding of the functions involved, practical examples you can copy‑paste, and answers to frequently asked questions.

No fluff here — just what actually works The details matter here..

Understanding Date Functions in SQL

Different SQL Dialects

SQL standards define a set of date functions, but each vendor extends the core with its own syntax. The most widely used families are:

  • MySQL – uses EXTRACT() and YEAR() / MONTH() functions.
  • PostgreSQL – relies on EXTRACT() and also provides DATE_PART().
  • SQL Server – employs DATEPART() and the YEAR() / MONTH() functions.
  • Oracle – utilizes EXTRACT() and the TO_CHAR() function for formatting.

Knowing which family your database belongs to helps you choose the right syntax and avoid errors.

Basic Syntax for Extracting Month and Year

Using EXTRACT

The EXTRACT function is part of the SQL standard and is supported by PostgreSQL, MySQL (8.0+), and Oracle. Its basic form is:

EXTRACT(unit FROM date_expression)
  • unit can be YEAR, MONTH, DAY, etc.
  • date_expression is a column or literal date value.

Using YEAR() and MONTH()

MySQL and SQL Server also expose dedicated functions:

  • MySQL: YEAR(date) and MONTH(date)
  • SQL Server: YEAR(date) and MONTH(date)

Using TO_CHAR (Oracle)

Oracle does not have a direct YEAR() function; instead you format the date string:

TO_CHAR(date_column, 'YYYY')   -- returns the year as a number
TO_CHAR(date_column, 'MM')     -- returns the month as a number

Step‑by‑Step Guide

Below are the concrete steps you can follow for each major SQL dialect.

1️⃣ MySQL

SELECT 
    YEAR(order_date)   AS order_year,
    MONTH(order_date)  AS order_month
FROM orders;
  • YEAR() returns an integer representing the year.
  • MONTH() returns a number from 1 (January) to 12 (December).

2️⃣ PostgreSQL

SELECT 
    EXTRACT(YEAR FROM order_date)  AS order_year,
    EXTRACT(MONTH FROM order_date) AS order_month
FROM orders;
  • EXTRACT(YEAR FROM ...) and EXTRACT(MONTH FROM ...) give the same integer values as MySQL’s functions.

3️⃣ SQL Server

SELECT 
    YEAR(order_date)  AS order_year,
    MONTH(order_date) AS order_month
FROM dbo.orders;
  • The syntax mirrors MySQL; YEAR() and MONTH() are deterministic and fast.

4️⃣ Oracle

SELECT 
    EXTRACT(YEAR FROM order_date)  AS order_year,
    EXTRACT(MONTH FROM order_date) AS order_month
FROM orders;
  • Oracle also supports EXTRACT, so you can use the standard form.
  • Alternatively, for formatting:
SELECT 
    TO_CHAR(order_date, 'YYYY')  AS order_year,
    TO_CHAR(order_date, 'MM')    AS order_month
FROM orders;

Common Use Cases and Practical Applications

  • Time‑based reporting – Grouping sales data by month and year helps generate monthly summaries.
  • Filtering recent records – WHERE order_date >= DATE_TRUNC('year', CURRENT_DATE) (PostgreSQL) or WHERE YEAR(order_date) = 2024 (MySQL) isolates the current year.
  • Calculating age – Subtract the extracted year from the current year to estimate age, though for precise age you should use months.

Example: Monthly Sales Report (MySQL)

SELECT 
    YEAR(sale_date)  AS year,
    MONTH(sale_date) AS month,
    SUM(amount)      AS total_sales
FROM sales
WHERE sale_date >= '2023-01-01'
GROUP BY YEAR(sale_date), MONTH(sale_date)
ORDER BY year, month;

This query aggregates sales for each month, producing a tidy table that can be exported to Excel or visualized in a chart.

FAQ

Q1: Can I get the month name instead of a number?

A: Yes. In MySQL you can use DATE_FORMAT(date, '%b')

A: Yes. In MySQL you can turn the numeric month into a textual representation with DATE_FORMAT().

  • Abbreviated name: DATE_FORMAT(order_date, '%b') → “Jan”, “Feb”, …
  • Full name: DATE_FORMAT(order_date, '%M') → “January”, “February”, …

You can also achieve the same result in Oracle by swapping the format model:

SELECT TO_CHAR(order_date, 'Mon') AS mon_name,
       TO_CHAR(order_date, 'Month') AS full_name
FROM orders;

Additional Frequently Asked Questions

Q2 – How can I retrieve the fiscal quarter instead of the raw month?
MySQL

SELECT 
    YEAR(order_date)          AS yr,
    QUARTER(order_date)       AS qtr,
    MONTH(order_date)         AS mon
FROM orders;

PostgreSQL

SELECT 
    EXTRACT(YEAR FROM order_date)  AS yr,
    EXTRACT(QUARTER FROM order_date) AS qtr,
    EXTRACT(MONTH FROM order_date)   AS mon
FROM orders;

SQL Server

SELECT 
    YEAR(order_date)  AS yr,
    DATEPART(QUARTER, order_date) AS qtr,
    MONTH(order_date)          AS mon
FROM dbo.orders;

Q3 – My dates are stored as strings (e.g., ‘2023‑07‑15’). How do I extract year and month?

  • MySQL: wrap the column with STR_TO_DATE() first.
    SELECT 
        YEAR(STR_TO_DATE(order_date_str, '%Y-%m-%d')) AS yr,
        MONTH(STR_TO_DATE(order_date_str, '%Y-%m-%d')) AS mon
    FROM sales;
    
  • PostgreSQL: cast the text to a timestamp.
    SELECT 
        EXTRACT(YEAR FROM order_date_str::date) AS yr,
        EXTRACT(MONTH FROM order_date_str::date) AS mon
    FROM sales;
    
  • SQL Server: use CAST or CONVERT.
    SELECT 
        YEAR(CONVERT(date, order_date_str)) AS yr,
        MONTH(CONVERT(date, order_date_str)) AS mon
    FROM dbo.sales;
    
  • Oracle: convert the character value with TO_DATE.
    SELECT 
        EXTRACT(YEAR FROM TO_DATE(order_date_str, 'YYYY-MM-DD'))
New on the Blog

Fresh from the Writer

If You're Into This

We Thought You'd Like These

Thank you for reading about Sql Query To Get Month And Year From Date. We hope the information has been useful. Feel free to contact us if you have any questions. See you next time — don't forget to bookmark!
⌂ Back to Home