SQL Query to Get Last 3 Months Records in SQL Server

← Prev

In SQL Server, you can use the DATEADD() function to get last 3 months (or n months) records. I have used this function in one of my previous posts where I have shown how easily you can get previous month records from the current month. Here in this post I’ll show you how to get last 3 months records or data in SQL Server.

Syntax DATEADD() function

The DATEADD() function returns a date time (smalldatetime). Its syntax is,

DATEDADD(interval, increment (int), expression (date))

This function is commonly used in date related queries.

Let’s create a table first in SQL Server. I have an employee table with few rows of data in it. The employee table has a date column (the last column). See the image.

Get last 3 months Records in SQL Server

The SQL Query to get Last 3 Months Records

WHERE JoiningDate >= DATEADD(M, -3, GETDATE())

The Output

Assuming that the current month is May.

Showing Last 3 Months Records in SQL Server

Its just a one like query. The function DATEADD() has 3 parameters.

The first parameter is the M, which denotes a month. You can replace the M with the MONTH. Like this,


The second parameter is the increment (an integer value or a number). I am using -3 to get the last 3 months records or data. You can use -5, -4 or any number, depending upon your requirement.

Thought you should know: You can use a variable to specify a number, and use the variable inside the DATEADD() function. Like this,

SET @D = 3

The third parameter inside the DATEADD() function, is an expression or date. I am using the GETDATE() function for current month.

Note: The DATEADD() function is also supported by Azure SQL Database.

Well, that’s it. Thanks for reading.

← Previous

Like this Article? Subscribe now, and get all the latest articles and tips, right in your inbox.
Delivered by FeedBurner