Click here to Skip to main content
15,889,867 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
hi all,
i have a two times StartTime and EndTime and i have to calculate total hours for these two time and i have sum up that total hours

Pls Help

Regards
Nirmala Saravanan
Posted

1 solution

DATEDIFF[^] will do the job for you.
SQL
SELECT DATEDIFF(hour, '2012-12-31 12:00:00.00', '2013-02-10 13:00:00.0000000');
will return 985, which is correct.

So supposing a fictive table structure since you have not specified any, this statement will summarize what the total hours:
SQL
select sum(DATEDIFF(hour, DateFiled1, DateField2)) from TableName


[Update based on new requirement]
To have a hh:mm format, you need to declare a function, or simply use following approach in your query:
SQL
DECLARE @d1 DATETIME
DECLARE @d2 DATETIME
SET @d1 = '2013-01-01 00:00'
SET @d2 = '2013-01-02 10:25'
SELECT DATEDIFF(minute, @d1, @d2)
SELECT CAST((DATEDIFF(minute, @d1, @d2) / 60) AS VARCHAR(8)) + ':' +
       right('0' + CAST((DATEDIFF(minute, @d1, @d2) % 60) AS VARCHAR(2)),2)

Of course, the last statement is the key, the rest is for demonstration.
 
Share this answer
 
v3
Comments
Nirmala Devi Saravanan 10-Feb-13 4:17am    
thanks for the reply

i want minutes also

Thanks

Nirmala Saravanan
Zoltán Zörgő 10-Feb-13 14:20pm    
See update. And next time try to be more precise in your original post.
Mehdi Gholam 10-Feb-13 14:25pm    
5'ed
Abhishek Pant 10-Feb-13 14:30pm    
good work,a 5
abdou_31 11-Mar-21 9:18am    
not working , where is the sum function in the last query?????????????????????????????

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900