Back
Can anyone tell me how to calculate time difference in hours and minutes in SQL Server?
We can be use CONVERT () function to calculate the time difference between two timestamps in hours and minutes.
Here is the example
Declare @Date_2 DATETIME = '2020-04-30 10:01:10.022'Declare @Date_1 DATETIME = '2020-04-30 10:00:00.000'Select CONVERT (TIME, @Date_2 - @Date_1) as Elapsed_Time
Declare @Date_2 DATETIME = '2020-04-30 10:01:10.022'
Declare @Date_1 DATETIME = '2020-04-30 10:00:00.000'
Select CONVERT (TIME, @Date_2 - @Date_1) as Elapsed_Time
The output for the above query would be:
To become an SQL expert, you can register for for the best SQL courses by Intellipaat that offers instructor-led training, certification, and job assistance
SELECT DATEDIFF(MINUTE, '2023-11-22 10:00:00', '2023-11-23 12:30:00') / 60 AS Hours, DATEDIFF(MINUTE, '2023-11-22 10:00:00', '2023-11-23 12:30:00') % 60 AS Minutes;
Here using datediff function.For hours,dividing with 60.
For minutes, modulo with 60
1.2k questions
2.7k answers
501 comments
693 users