42

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)'
4

12 回答 12

65

您可以将持续时间转换为日期,然后对其进行格式化:

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 */

为了降低精度,修改 varchar 的大小:

SELECT CONVERT(varchar(5), 
       DATEADD(minute, DATEDIFF(minute, @FirstDate, @LastDate), 0), 114) 

/* Results: 02:30 */
于 2013-07-18T21:03:08.837 回答
21

此功能是将持续时间以分钟为单位转换为可读的小时和分钟格式。即2h30m。如果持续时间少于一小时,它会消除小时数,如果持续时间以小时为单位,则仅显示小时数而没有额外的分钟数。

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

要使用此功能:

SELECT dbo.MinutesToDuration(23)

结果:23m

SELECT dbo.MinutesToDuration(120)

结果:2小时

SELECT dbo.MinutesToDuration(147)

结果:2h27m

希望这可以帮助!

于 2013-11-10T07:58:54.147 回答
9

我不确定这些是最好的选择,但它们肯定会完成工作:

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
于 2013-07-18T21:03:22.743 回答
6
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 : */
于 2017-01-02T16:13:04.690 回答
3

对于那些需要将分钟转换为超过 24 小时格式的时间的人:

DECLARE @minutes int = 7830
SELECT CAST(@minutes / 60 AS VARCHAR(8)) + ':' + FORMAT(@minutes % 60, 'D2') AS [Time]

结果:

130:30
于 2019-12-11T07:22:01.280 回答
2

感谢 A Ghazal,正是我所需要的。这是他(她)答案的稍微清理过的版本:

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
于 2015-08-12T15:49:27.367 回答
2

这似乎对我有用:

SELECT FORMAT(@mins / 60 * 100 + @mins % 60, '#:0#')

于 2020-10-01T11:51:00.217 回答
1
select convert(varchar(5),dateadd(mi,DATEDIFF(minute, FirstDate,LastDate),'00:00'),114)    
于 2017-05-14T08:36:15.213 回答
1

如果有人有兴趣获得结果,因为 60 变为 01:00,120 变为 02:00,150 变为 02:30,此功能可能会有所帮助:

    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
于 2019-10-14T05:26:50.120 回答
0

我会执行以下操作(将下面的全部内容复制粘贴到即时窗口/查询窗口中并执行)

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存储您在问题中生成的值。到时候“技巧”就来了:
我们创建一个smalldatetime变量(在我的例子中是yyyy.mm.dd格式)并用你的int值递增它,然后只显示(或存储)time部分。

于 2013-07-18T21:10:53.017 回答
0
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
于 2019-07-09T01:47:15.923 回答
0

如何在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;

于 2020-01-20T07:48:57.967 回答