Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Converting minutes into HH:MM

Author  Topic 

nevzab
Starting Member

34 Posts

Posted - 2013-06-20 : 17:33:46
I need to convert minutes into HH:MM.

For example:

60 = 01:00
65 = 01:05
70 = 01:10
100 = 01:40
120 = 02:00
2880 = 48:00
2945 = 49:05

What is the simplest way to do this?

Cheers,

Nev.

James K
Master Smack Fu Yak Hacker

3873 Posts

Posted - 2013-06-20 : 17:52:57
May not be the simplest, but here is an example of one way to do this:
DECLARE @x INT = 2945;
SELECT @x/60, @x%60,
RIGHT('00'+CAST(@x/60 AS VARCHAR(8)),2)+':'+RIGHT('00'+CAST(@x%60 AS VARCHAR(8)),2)
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-06-21 : 02:10:39
[code]
SELECT CAST((DATEDIFF(dd,0,dateval) *24 + DATEPART(hh,dateval)) AS varchar(3)) + ':' + DATENAME(minute,dateval)
FROM (SELECT DATEADD(minutes,yourfield,0) AS dateval FROM table)t
[/code]

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page

nevzab
Starting Member

34 Posts

Posted - 2013-06-24 : 17:44:53
Guys, I forgot to thank you your answers. Genius!!
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-06-25 : 00:38:10
no problem
you're welcome

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page

ditch
Master Smack Fu Yak Hacker

1466 Posts

Posted - 2013-06-25 : 02:09:36
Visakh - since our discussion on some other thread last week about datetime functions wrt allowing any indexes to still be used, you have converted me to use these datetime calculations as much as possible as opposed to MONTH and Year type functions.

I had never given this side of sql any thought until you pointed this out to me.
Thanks again.

Duane.
http://ditchiecubeblog.wordpress.com/
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-06-25 : 02:16:43
quote:
Originally posted by ditch

Visakh - since our discussion on some other thread last week about datetime functions wrt allowing any indexes to still be used, you have converted me to use these datetime calculations as much as possible as opposed to MONTH and Year type functions.

I had never given this side of sql any thought until you pointed this out to me.
Thanks again.

Duane.
http://ditchiecubeblog.wordpress.com/


No problem
Glad that my post could be of help to you

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page
   

- Advertisement -