Converting minutes to days and hours

  • Dear All,

    Is there any funtion that returns days and hours on passing of minutes to it.

    For example if pass 2945 minutes it should return me 1 day 1 hour and 5 minutes.

    Please help me to solve this

  • keep in mind the number of rocket scientists is not that high 😉

    Declare @theMinutes int

    Set @theMinutes = 2945

    Select @theMinutes / 1440 as NoDays -- 1440 minutes per day

    , (@theMinutes % 1440) / 60 as NoHours -- modulo 1440

    , (@theMinutes % 60) as NoMinutes -- modulo 60

    Johan

    Learn to play, play to learn !

    Dont drive faster than your guardian angel can fly ...
    but keeping both feet on the ground wont get you anywhere :w00t:

    - How to post Performance Problems
    - How to post data/code to get the best help[/url]

    - How to prevent a sore throat after hours of presenting ppt

    press F1 for solution, press shift+F1 for urgent solution 😀

    Need a bit of Powershell? How about this

    Who am I ? Sometimes this is me but most of the time this is me

  • Thanks for your reply

    Can i have output in one column rather than 3 colums

  • [font="Verdana"]

    Yes, just concatenate the columns.

    Declare @theMinutes int

    Set @theMinutes = 2945

    Select @theMinutes / 1440 as NoDays + ' ' + (@theMinutes % 1440) / 60 as NoHours + ' ' + (@theMinutes % 60) as NoMinutes

    Mahesh

    [/font]

    MH-09-AM-8694

  • There is a little typo in the concatenations ( remove "as colname")

    i've also added leading zeros to hours and minutes.

    it should be :

    Declare @theMinutes int

    Set @theMinutes = 2945

    Select convert(varchar(15), @theMinutes / 1440 )

    + ' ' + REPLICATE('0', 2 - DATALENGTH(convert(varchar(2), (@theMinutes % 1440) / 60 ))) + convert(varchar(2), (@theMinutes % 1440) / 60 )

    + ' ' + REPLICATE('0', 2 - DATALENGTH(convert(varchar(2), (@theMinutes % 60)))) + convert(varchar(2), (@theMinutes % 60)) as Days_Hours_Minutes

    Johan

    Learn to play, play to learn !

    Dont drive faster than your guardian angel can fly ...
    but keeping both feet on the ground wont get you anywhere :w00t:

    - How to post Performance Problems
    - How to post data/code to get the best help[/url]

    - How to prevent a sore throat after hours of presenting ppt

    press F1 for solution, press shift+F1 for urgent solution 😀

    Need a bit of Powershell? How about this

    Who am I ? Sometimes this is me but most of the time this is me

Viewing 5 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic. Login to reply