Difference B/W Two Dates In SQL: What Are The Different Methods?
I am doing a project related to SQL. I am stuck with the problem of how to find the difference between two dates in SQL. Which commands to use?
I am doing a project related to SQL. I am stuck with the problem of how to find the difference between two dates in SQL. Which commands to use?
The DATEDIFF() returns the difference between two dates in SQL.
Syntax:
DATEDIFF(depart, start date, end date)
Example:
SELECT DATEDIFF(day, '2016-05-05', '2016-07-05') AS DiffDate
Result:
DiffDate
61
This syntax yielded result in positive. In this syntax, the first date is earlier than the second date.
Syntax:
DATEDIFF(depart, end date, start date)
Example:
SELECT DATEDIFF(day, '2016-07-05', '2016-05-05') AS DiffDate
Result:
DiffDate
-61
This syntax yielded result in negative. In this syntax, the first date is afterward that of the second date.