Click here to Skip to main content
15,892,737 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all,
I have a select query that has DURATION column to calculate number of Minutes . I want to convert those minutes to hh:mm format.


Duration has values like 60, 120,150


For example:

510 becomes 08:5 hours


i have tried below query but am not getting the exact value.

select  ISNULL(CONVERT(VARCHAR,CONVERT(INT,510)/60)+':'+ RIGHT('0'+CONVERT(VARCHAR,CONVERT(INT,510)%60),2),0) AS TotalHour


my expected out put is:
8:5

please guide me were i made mistake.

thanks to all.

regards,
stellus

What I have tried:

How to convert number of minutes to hh:mm format in TSQL?
Posted
Updated 11-Jul-18 23:38pm

Please see below link for a similar question asked and have good answers.

sql server 2008 - How to convert number of minutes to hh:mm format in TSQL? - Stack Overflow[^]
 
Share this answer
 
select left(convert(time,DATEADD(minute,[column_name],0)),5) as [Time] from [tbl_name]
 
Share this answer
 
SQL
DECLARE @tm INT;

SELECT @tm =SUM(tm) FROM (
                          SELECT 60 AS tm UNION SELECT 120 UNION  SELECT 150 
                         )AS hm

SELECT format(DATEADD(MINUTE,@tm,CAST(CAST(0 AS FLOAT) AS DATETIME)),'HH:mm');
 
Share this answer
 

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