Click here to Skip to main content
15,888,351 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi
I have a table in time with two Columns hours (decimal) and minutes (int)

i need to find the sum of the data in hours and minutes (HH:MM)

I have written the query

SQL
(Convert (int, (SUM (hours )
        + ( SUM (minutes ) / 60 )
        + ( SUM (minutes ) % 60/ 60))))) +'.'+
    (Convert (varchar (20), ( SUM (minutes ) % 60 ) % 60)))
From time



But the problem is when the value is n single digit in min like(0,1,2,...,9) then the output comes like eg
hrs=5
mins=7
Then the output is 5.7
But the output should come 5.07

Can you please help me out with it

Thank you
Posted
Updated 25-Nov-13 0:30am
v3

SQL
SELECT CAST( @Minute1/60 AS VARCHAR(5))+ ' HH' + '.'+ RIGHT('0' + CAST( @Minute1%60 AS VARCHAR(2)), 2)+' MM' AS 'WorkingTime'
 
Share this answer
 
Comments
write2varun 25-Nov-13 6:44am    
hithaks for the quick reply but i am getting error
"Arithmetic overflow error converting numeric to data type varchar."
SQL
Declare @Minute Int = 560
SELECT CAST( @Minute/60 AS Varchar(20))+ ' HH' + '.'+ RIGHT('0' + CAST( @Minute%60 AS VARCHAR(20)), 2)+' MM' AS 'WorkingTime'
 
Share this answer
 
Comments
write2varun 25-Nov-13 7:22am    
but my hours are like 5hrs.3mins

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