In SQL, DATEDIFF function is used to find the difference between two dates.
DATEDIFF(datepart, startdate, enddate)
• Specify the 'datepart' that indicates the specific portion of the date or time to calculate the difference for. i.e. second, minutes, hour, day, week, month, year, etc.
• Specify the 'startdate' that you want to calculate the difference.
• Specify the 'enddate' that you want to calculate the difference.
1. Let's assume we have a table named "[Employees]".
2. Let's assume you want to find the YEAR difference between Joining_Date and Resign_Date column for each employee.
3. Run below SQL statement :
SELECT *, DATEDIFF(YEAR, Joining_Date, Resign_Date) AS Total_Service FROM [Employees];
4. In above statement, DATEDIFF function calculates year difference between Joining_Date and Resign_Date column for each employee and returns result in a column named 'Total_Service'.
Using this function in a select statement won't modify the [Employees] table directly, but it will only be reflected in the select statement's output.
DATEDIFF function in SQL calculates the difference between two dates.
To calculate the difference between two dates in SQL, use the DATEDIFF function.
The three arguments for DATEDIFF in SQL are: start date, end date, and the date part (e.g., day, month, year).
To convert DATEDIFF result to a date in SQL, you typically add or subtract the difference from a date using date manipulation functions like DATEADD.