Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in SQL by (55.6k points)

Can anyone tell me how to calculate time difference in hours and minutes in SQL Server?

2 Answers

0 votes
by (119k points)
edited by

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

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

0 votes
by (1.5k points)

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

Related questions

1.2k questions

2.7k answers

501 comments

693 users

Browse Categories

...