Reputation: 4004
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)'
Upvotes: 46
Views: 226326
Reputation: 1
declare @TimeFormat nvarchar(20),@minutes BIGINT
SET @minutes=125
SET @TimeFormat =
CASE WHEN @minutes >= 60 THEN (SELECT
CASE WHEN CAST((@minutes / 60) AS VARCHAR(2))<10 THEN '0' + CAST((@minutes / 60) AS VARCHAR(2)) ELSE CAST((@minutes / 60) AS VARCHAR(2)) END
+ ':' +
CASE WHEN (@minutes % 60) > 0
THEN CASE WHEN CAST((@minutes % 60) AS VARCHAR(2))<10 THEN '0'+CAST((@minutes % 60) AS VARCHAR(2)) ELSE CAST((@minutes % 60) AS VARCHAR(2)) END
ELSE '00'
END)
ELSE CASE WHEN CAST((@minutes % 60) AS VARCHAR(2))<10 THEN '00:0'+CAST((@minutes % 60) AS VARCHAR(2)) ELSE '00:'+CAST((@minutes % 60) AS VARCHAR(2)) END
END
select @TimeFormat
Upvotes: 0
Reputation: 811
If you want a notation of XX days YY hours and ZZ min, just try:
SELECT
CAST(f.TimeAmount / 1440 AS VARCHAR(8)) + 'd ' +
CAST((f.TimeAmount % 1440) / 60 AS VARCHAR(8)) + 'h ' +
FORMAT(f.TimeAmount % 60, 'D2') + 'min' AS [TIME_TEXT]
FROM
MyTable f
Upvotes: 0
Reputation: 1086
This seems to work for me:
SELECT FORMAT(@mins / 60 * 100 + @mins % 60, '#:0#')
Upvotes: 5
Reputation: 19
How to get the First and Last Record time different in sql server....
....
Select EmployeeId,EmployeeName,AttendenceDate,MIN(Intime) as Intime ,MAX(OutTime) as OutTime,
DATEDIFF(MINUTE, MIN(Intime), MAX(OutTime)) as TotalWorkingHours
FROM ViewAttendenceReport WHERE AttendenceDate >='1/20/2020 12:00:00 AM' AND AttendenceDate <='1/20/2020 23:59:59 PM'
GROUP BY EmployeeId,EmployeeName,AttendenceDate;
Upvotes: 0
Reputation: 591
For those who need convert minutes to time with more than 24h format:
DECLARE @minutes int = 7830
SELECT CAST(@minutes / 60 AS VARCHAR(8)) + ':' + FORMAT(@minutes % 60, 'D2') AS [Time]
Result:
130:30
Upvotes: 9
Reputation: 113
In case someone is interested in getting results as 60 becomes 01:00 hours, 120 becomes 02:00 hours, 150 becomes 02:30 hours, this function might help:
create FUNCTION [dbo].[MinutesToHHMM]
(
@minutes int
)
RETURNS varchar(30)
AS
BEGIN
declare @h int
set @h= @minutes / 60
declare @mins varchar(2)
set @mins= iif(@minutes%60<10,concat('0',cast((@minutes % 60) as varchar(2))),cast((@minutes % 60) as varchar(2)))
return iif(@h <10, concat('0', cast(@h as varchar(5)),':',@mins)
,concat(cast(@h as varchar(5)),':',@mins))
end
Upvotes: 1
Reputation: 382
declare function dbo.minutes2hours (
@minutes int
)
RETURNS varchar(10)
as
begin
return format(dateadd(minute,@minutes,'00:00:00'), N'HH\:mm','FR-fr')
end
Upvotes: 0
Reputation: 493
select convert(varchar(5),dateadd(mi,DATEDIFF(minute, FirstDate,LastDate),'00:00'),114)
Upvotes: 1
Reputation: 81
DECLARE @Duration int
SET @Duration= 12540 /* for example big hour amount in minutes -> 209h */
SELECT CAST( CAST((@Duration) AS int) / 60 AS varchar) + ':' + right('0' + CAST(CAST((@Duration) AS int) % 60 AS varchar(2)),2)
/* you will get hours and minutes divided by : */
Upvotes: 8
Reputation: 21
Thanks to A Ghazal, just what I needed. Here's a slightly cleaned up version of his(her) answer:
create FUNCTION [dbo].[fnMinutesToDuration]
(
@minutes int
)
RETURNS nvarchar(30)
-- Based on http://stackoverflow.com/questions/17733616/how-to-convert-number-of-minutes-to-hhmm-format-in-tsql
AS
BEGIN
return rtrim(isnull(cast(nullif((@minutes / 60)
, 0
) as varchar
) + 'h '
,''
)
+ isnull(CAST(nullif((@minutes % 60)
,0
) AS VARCHAR(2)
) + 'm'
,''
)
)
end
Upvotes: 2
Reputation: 2813
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.
CREATE FUNCTION [dbo].[MinutesToDuration]
(
@minutes int
)
RETURNS nvarchar(30)
AS
BEGIN
declare @hours nvarchar(20)
SET @hours =
CASE WHEN @minutes >= 60 THEN
(SELECT CAST((@minutes / 60) AS VARCHAR(2)) + 'h' +
CASE WHEN (@minutes % 60) > 0 THEN
CAST((@minutes % 60) AS VARCHAR(2)) + 'm'
ELSE
''
END)
ELSE
CAST((@minutes % 60) AS VARCHAR(2)) + 'm'
END
return @hours
END
To use this function :
SELECT dbo.MinutesToDuration(23)
Results: 23m
SELECT dbo.MinutesToDuration(120)
Results: 2h
SELECT dbo.MinutesToDuration(147)
Results: 2h27m
Hope this helps!
Upvotes: 24
Reputation:
I would do the following (copy-paste the whole stuff below into immediate window / query window and execute)
DECLARE @foo int
DECLARE @unclefoo smalldatetime
SET @foo = DATEDIFF(minute, CAST('2013.01.01 00:00:00' AS datetime),CAST('2013.01.01 00:03:59' AS datetime)) -- AS 'Duration (Minutes)'
SET @unclefoo = DATEADD(minute, @foo, '2000.01.01')
SELECT CAST(@unclefoo AS time)
@foo
stores the value you generate in your question. The "trick" comes by then:
we create a smalldatetime
variable (in my case it's yyyy.mm.dd
format) and increment it with your int
value, then display (or store if you want) the time
part only.
Upvotes: 0
Reputation: 175
I'm not sure these are the best options but they'll definitely get the job done:
declare @durations table
(
Duration int
)
Insert into @durations(Duration)
values(60),(80),(90),(150),(180),(1000)
--Option 1 - Manually concatenate the values together
select right('0' + convert(varchar,Duration / 60),2) + ':' + right('0' + convert(varchar,Duration % 60),2)
from @Durations
--Option 2 - Make use of the time variable available since SQL Server 2008
select left(convert(time,DATEADD(minute,Duration,0)),5)
from @durations
GO
Upvotes: 12
Reputation: 11406
You can convert the duration to a date and then format it:
DECLARE
@FirstDate datetime,
@LastDate datetime
SELECT
@FirstDate = '2000-01-01 09:00:00',
@LastDate = '2000-01-01 11:30:00'
SELECT CONVERT(varchar(12),
DATEADD(minute, DATEDIFF(minute, @FirstDate, @LastDate), 0), 114)
/* Results: 02:30:00:000 */
For less precision, modify the size of the varchar:
SELECT CONVERT(varchar(5),
DATEADD(minute, DATEDIFF(minute, @FirstDate, @LastDate), 0), 114)
/* Results: 02:30 */
Upvotes: 70