How to Find Employees Who were Hired in the Last n Months in SQL Server

← PrevNext →

Last updated: 4th February 2025

The DATEADD() function in SQL Server is a powerful tool that allows you to manipulate dates. One common use case is to find employees who have been hired in the last 'n' months. The value of 'n' can be any number you choose. Let's explore some examples.

Create a Table in SQL Server

This is my employee table with few rows in it.

Sample Employee Table

Or, you can use this sample Employee table. Create the table.

Find Employees Hired in the Last n Months

Here’s the query.

DECLARE @n INT
SET @n = 3   -- Change this value to any other number
SELECT * FROM dbo.Employees
WHERE DOJ >= DATEADD(M, -@n, GETDATE())

I am using a variable to define the number of months, since its n months. So, the value can be anything like 3, 4, 6 etc.

This query allows you to set the value of "n" dynamically. You can execute the query using a Stored Procedure.

Here’s the output.

Employees who were Hired in the last n Months

Find Employees who were Hired in the Last 4 Months

Well, you can even use this query, if you know the number of months.

SELECT *FROM dbo.Employees
WHERE DOJ >= DATEADD(MONTH, -4,GETDATE())

Find employees who were Hired in the last 12 months

SELECT EmployeeName, DOJ
FROM dbo.Employees
WHERE DOJ >= DATEADD(month, -12, GETDATE());

Now, let’s understand the SQL function, which is responsible for this output.

DATEADD() function

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

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

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

← PreviousNext →