---
title: "How to find the difference between two given dates in SQL Server?"  
description: "How to find the difference between two given dates in SQL Server?"  
author: "Ashutosh Kumar Verma"  
published: 2021-11-22  
updated: 2023-06-20  
canonical: https://www.mindstick.com/interview/33843/how-to-find-the-difference-between-two-given-dates-in-sql-server  
category: "mssql server"  
tags: ["sql server", "sql"]  
reading_time: 2 minutes  

---

# How to find the difference between two given dates in SQL Server?

## DATEDIFF():

SQL **DATEDIFF()** function is used to find the difference between two given dates in term of year, month or day.

## Example:

Find the different between two date in Year.

```
    SELECT DATEDIFF(YEAR,'2011/12/15','2013/05/25');
```

**Output:** 2

Find the different between two date in Month.

```
SELECT DATEDIFF(MONTH,'2011/12/15','2013/05/25');
```

**Output:** 17

Find the different between two date in Day.

```
SELECT DATEDIFF(DAY,'2011/12/15','2013/05/25');
```

**Output:** 527

## Answers

### Answer by Aryan Kumar

To find the difference between two given dates in SQL Server, you can use the DATEDIFF() function. The DATEDIFF() function takes three arguments:

- The first argument is the datepart, which specifies the unit of time that you want to measure the difference in. For example, you can use day, month, year, hour, minute, or second.
- The second argument is the start date.
- The third argument is the end date.

The DATEDIFF() function returns the difference between the two dates in the specified unit of time. For example, the following query will return the difference between the dates '2023-01-01' and '2023-01-15' in days:

SQL

```plaintext
SELECT DATEDIFF(day, '2023-01-01', '2023-01-15');
```

This query will return the value 14, which means that there are 14 days between the two dates.

Here is the syntax for the DATEDIFF() function:

SQL

```plaintext
DATEDIFF ( datepart, start_date, end_date )
```

- datepart is the datepart that you want to measure the difference in.
- start_date is the start date.
- end_date is the end date.

The DATEDIFF() function can also be used to find the difference between two datetimes. For example, the following query will return the difference between the datetimes '2023-01-01 12:00:00' and '2023-01-15 12:00:00' in hours:

SQL

```plaintext
SELECT DATEDIFF(hour, '2023-01-01 12:00:00', '2023-01-15 12:00:00');
```

This query will return the value 336, which means that there are 336 hours between the two datetimes.

### Answer by Ashutosh Kumar Verma

## DATEDIFF():

SQL **DATEDIFF()** function is used to find the difference between two given dates in term of year, month or day.

## Example:

Find the different between two date in Year.

```
    SELECT DATEDIFF(YEAR,'2011/12/15','2013/05/25');
```

**Output:** 2

Find the different between two date in Month.

```
SELECT DATEDIFF(MONTH,'2011/12/15','2013/05/25');
```

**Output:** 17

Find the different between two date in Day.

```
SELECT DATEDIFF(DAY,'2011/12/15','2013/05/25');
```

**Output:** 527


---

Original Source: https://www.mindstick.com/interview/33843/how-to-find-the-difference-between-two-given-dates-in-sql-server

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
