SQL DATEDIFF: Calculate Time Difference

In SQL, the DATEDIFF function can be used to calculate the difference in time between two timestamps. The specific syntax is:

SELECT DATEDIFF('unit', start_date, end_date);

‘unit’ represents the time interval unit to be calculated, which can be one of the following values: YEAR, MONTH, DAY, HOUR, MINUTE, SECOND. ‘start_date’ and ‘end_date’ respectively represent the starting timestamp and ending timestamp.

For example, you can use the following SQL statement to calculate the difference in days between two timestamps:

SELECT DATEDIFF(DAY, '2022-01-01', '2022-01-10');

This statement will return 9, indicating a 9-day difference between the two timestamps.

bannerAds