---
title: "How to write SQL Query to get the value of the previous month and year?"  
description: "How to write SQL Query to get the value of the previous month and year?"  
author: "Steilla Mitchel"  
published: 2023-07-30  
updated: 2023-07-31  
canonical: https://www.mindstick.com/forum/159363/how-to-write-sql-query-to-get-the-value-of-the-previous-month-and-year  
category: "mssql server"  
tags: ["mssql server", "sql server", "sql"]  
reading_time: 2 minutes  

---

# How to write SQL Query to get the value of the previous month and year?

How to write [SQL Query](https://www.mindstick.com/forum/529/rename-table-name-and-column-name-using-sql-query) to get the [value](https://www.mindstick.com/articles/23219/an-optimized-description-adds-value-to-experience-and-in-turn-effectively-guest-posting-packages) of the [previous](https://yourviews.mindstick.com/view/81421/unlock-2-0-is-just-like-previous-corona-lockdowns) month and year?

## Replies

### Reply by Aryan Kumar

Sure, here is how to write a [SQL](https://www.mindstick.com/articles/13115/types-of-keys-in-sql-or-oracle-database) [query](https://www.mindstick.com/blog/202/sub-query-in-sqlserver) to get the value of the previous month and year:

SQL

```plaintext
SELECT
  MONTH(CURRENT_DATE() - INTERVAL 1 MONTH) AS previous_month,
  YEAR(CURRENT_DATE() - INTERVAL 1 MONTH) AS previous_year;
```

This query will first calculate the current date and then subtract 1 month from it. The `MONTH()` function will return the month number of the date, and the `YEAR()` function will return the year number of the date.

The results of the query will be the previous month and year. For example, if the current date is January 1, 2023, then the results of the query will be 12 and 2022.

Here is an explanation of the query:

- The `SELECT` statement specifies the columns that you want to return. In this case, you want to return the `previous_month` and `previous_year` columns.
- The `CURRENT_DATE()` function returns the current date.
- The `INTERVAL 1 MONTH` expression creates a time interval that is 1 month long.
- The `-` operator subtracts the time interval from the current date.
- The `MONTH()` function returns the month number of the date.
- The `YEAR()` function returns the year number of the date.


---

Original Source: https://www.mindstick.com/forum/159363/how-to-write-sql-query-to-get-the-value-of-the-previous-month-and-year

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
