How to split minutes into days, hours and minutes in tsql

cihadakt picture cihadakt · May 3, 2013 · Viewed 17.2k times · Source

I have a column which consist of minutes. Is there any simple way to split minutes column into one column which shows days, hours, minutes only?

DURATION              
-----------
67           ==> 1 hour, 7 minutes
1507         ==> 1 day, 1 hour, 7 minutes
23           ==> 23 minutes

I googled for solution but I didn't find any solution similar for me. I want to show this in a report but trying to find how can I solve this column looks meaningfully. Duration column's calculation like this.

avg(datediff(MINUTE, ei.SentDate, eo.SentDate)) over(partition by ei.mailbox) as DURATION   

Answer

Vishwanath Dalvi picture Vishwanath Dalvi · May 3, 2013

A google search landed me here http://www.sqlservercentral.com/Forums/Topic490411-8-1.aspx

and It says... which I just tested working ...

Declare @theMinutes int
Set @theMinutes = 67 

Select @theMinutes / 1440 as NoDays  -- 1440 minutes per day 
       , (@theMinutes % 1440) / 60 as NoHours -- modulo 1440 
       , (@theMinutes % 60) as NoMinutes -- modulo 60