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:
60 becomes 01:00 hours
120 becomes 02:00 hours
150 becomes 02:30 hours
Also, this is how I retrieve DURATION (Minutes
)
DATEDIFF(minute, FirstDate,LastDate) as 'Duration (Minutes)'
Thanks to A Ghazal, just what I needed. Here's a slightly cleaned up version of his(her) answer:
You can convert the duration to a date and then format it:
For less precision, modify the size of the varchar:
This function is to convert duration in minutes to readable hours and minutes format. i.e 2h30m. It eliminates the hours if the duration is less than one hour, and shows only the hours if the duration in hours with no extra minutes.
To use this function :
Results: 23m
Results: 2h
Results: 2h27m
Hope this helps!
I would do the following (copy-paste the whole stuff below into immediate window / query window and execute)
@foo
stores the value you generate in your question. The "trick" comes by then:we create a
smalldatetime
variable (in my case it'syyyy.mm.dd
format) and increment it with yourint
value, then display (or store if you want) thetime
part only.EX: select minutesToHHMM(1441) returns 24h1m