How To Get Previous Month Data From Current Date In SQL Server

In this comprehensive article, I will walk you through multiple proven methods for retrieving the previous month’s data in SQL Server, complete with practical examples.

How To Get Previous Month Data From Current Date In SQL Server

Now, let’s explore the various techniques to retrieve the previous month’s data in SQL Server.

Method 1: Using DATEADD and EOMONTH Functions

The most straightforward approach is to combine the DATEADD and EOMONTH functions.

SELECT 
    DATEADD(DAY, 1, EOMONTH(GETDATE(), -2)) AS FirstDayOfPreviousMonth,
    EOMONTH(GETDATE(), -1) AS LastDayOfPreviousMonth

This query works by calculating the end of the month before last (using EOMONTH with -2), then adding one day to get the first day of the previous month. For the last day, we simply use EOMONTH with -1.

Let’s break down how this works:

  1. GETDATE() returns the current date (e.g., April 28, 2025)
  2. EOMONTH(GETDATE(), -1) returns March 31, 2025 (last day of previous month)
  3. EOMONTH(GETDATE(), -2) returns February 28, 2025 (last day of month before last)
  4. DATEADD(DAY, 1, EOMONTH(GETDATE(), -2)) returns March 1, 2025 (first day of previous month)

After executing the above query, I got the expected output as shown in the screenshot below.

How To Get Previous Month Data From Current Date In SQL Server

Example

Here’s how you might use this to query actual data. The below query will return all sales from the previous month.

SELECT 
    sale_date, 
    sale_amount,  
    TotalSales
FROM 
    Sales
WHERE 
    sale_date BETWEEN 
        DATEADD(DAY, 1, EOMONTH(GETDATE(), -2)) AND 
        EOMONTH(GETDATE(), -1)
ORDER BY 
    sale_date

After executing the above query, I got the expected output as shown in the screenshot below.

Get Previous Month Data From Current Date In SQL Server

Method 2: Using DATEADD with DATEDIFF

If you’re working with an older version of SQL Server that doesn’t support EOMONTH, or if you prefer a different approach, the DATEADD and DATEDIFF combination offers a robust alternative.

SELECT
    DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE())-1, 0) AS FirstDayOfPreviousMonth,
    DATEADD(DAY, -1, DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()), 0)) AS LastDayOfPreviousMonth

This method works by:

  1. Using DATEDIFF to calculate the number of months between January 1, 1900 (represented by 0) and the current date
  2. Subtracting 1 to get the previous month
  3. Using DATEADD to find the first day of the previous month
  4. Finding the last day by calculating the first day of the current month and subtracting one day

After executing the above query, I got the expected output.

get previous month from current date in sql

Example

This query retrieves the total sales by sale date for the previous month, sorted by performance.

SELECT 
    sale_date, 
    SUM(sale_amount) AS MonthlySales
FROM 
    Sales
WHERE 
    sale_date BETWEEN 
        DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE())-1, 0) AND 
        DATEADD(DAY, -1, DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()), 0))
GROUP BY 
    sale_date
ORDER BY 
    MonthlySales DESC

After executing the above query, I got the expected output as shown in the screenshot below.

How To Get Previous Month Data From Current Date In SQL

Method 3: Using Month and Year Comparisons

Sometimes, you may want to filter by month and year rather than exact dates. You can use the below query for that purpose. This approach extracts the month from a date that’s one month before today, then filters records where both month and year match.

SELECT 
    sale_amount
FROM 
    Sales
WHERE 
    MONTH(sale_date) = MONTH(DATEADD(MONTH, -1, GETDATE())) AND
    YEAR(sale_date) = YEAR(DATEADD(MONTH, -1, GETDATE()))

After executing the above query, I got the expected output.

How To Get Previous Month Data From Current Date SQL

Method 4: Using DATEADD with EOMONTH Functions in a different way like Oracle

We can use the following query.

SELECT 
    DATEADD(DAY, 1, EOMONTH(GETDATE(), -2)) AS FirstDayPrevMonth,
    EOMONTH(GETDATE(), -1) AS LastDayPrevMonth

After executing the above query, I got the expected output as shown in the screenshot below.

Get Previous Month Data From Current Date SQL Server

Conclusion

Retrieving the previous month’s data is essential for business analysis, reporting, and data monitoring. Knowing how to query previous month’s data in SQL Server effectively is straightforward using the approaches mentioned in this article.

You may also like the following articles.