March 16, 2009

Convert Number to Hours and Minutes in SQL Server




In one of my previous posts http://www.sqlservercurry.com/2009/03/convert-integer-to-date-and-time-in-sql.html, I had demonstrated how to convert integer to Date and Time. I got a mail from one of my readers who asked me if I could calculate hours and seconds from an integer field.

Here's how to do so:


DROP Table #SampleTable


CREATE Table #SampleTable (


[numasdate] int


);


 


Insert Into #SampleTable Values('9436');


Insert Into #SampleTable Values('537');


Insert Into #SampleTable Values('9323');


Insert Into #SampleTable Values('12525');


 


-- To calculate Total Hours and Minutes


Select numasdate,


Cast(numasdate / 60 as Varchar) + ' hours ' +


Cast(numasdate % 60 as Varchar) + ' minutes'


as [TotalHoursAndMinutes]


From


#SampleTable




Output:


9436    157 hours 16 minutes


537        8 hours 57 minutes


9323    155 hours 23 minutes


12525    208 hours 45 minutes



Did you like this post?
kick it on DotNetKicks.com
subscribe via rss subscribe via e-mail
print this post follow me on twitter


About The Author

Suprotim Agarwal, ASP.NET Architecture MVP works as an Architect Consultant and provides consultancy on how to design and develop Web applications.

Suprotim is also the founder and primary contributor to DevCurry, DotNetCurry and SQLServerCurry. He has also written an EBook 51 Recipes using jQuery with ASP.NET Controls.

Follow him on twitter @suprotimagarwal

 
  Feedback:

comments

1 Response to "Convert Number to Hours and Minutes in SQL Server"
  1. Rupert Ralston said...
    October 18, 2011 at 6:25 AM

    Thank you, so much rubbish to sort through on the web finding this site was amazing, worked first time and a nice query, cheers.

 

Copyright © 2009-2014 All Rights Reserved for SQLServerCurry.com by Suprotim Agarwal | Terms and Conditions