如何在sql中将分钟分成几天,几小时和几分钟

时间:2013-05-03 06:02:13

标签: sql-server tsql datetime date-conversion

我有一个由分钟组成的专栏。有没有简单的方法将分钟列分成一列,只显示天,小时,分钟?

DURATION              
-----------
67           ==> 1 hour, 7 minutes
1507         ==> 1 day, 1 hour, 7 minutes
23           ==> 23 minutes

我用谷歌搜索解决方案,但我没有找到类似的解决方案。我想在一份报告中展示这一点,但试图找到如何解决这一专栏看起来有意义。持续时间列的计算方式如下。

avg(datediff(MINUTE, ei.SentDate, eo.SentDate)) over(partition by ei.mailbox) as DURATION   

2 个答案:

答案 0 :(得分:6)

谷歌搜索让我来到这里http://www.sqlservercentral.com/Forums/Topic490411-8-1.aspx

并且它说...我刚刚测试了......

Declare @theMinutes int
Set @theMinutes = 67 

Select @theMinutes / 1440 as NoDays  -- 1440 minutes per day 
       , (@theMinutes % 1440) / 60 as NoHours -- modulo 1440 
       , (@theMinutes % 60) as NoMinutes -- modulo 60

答案 1 :(得分:2)

create FUNCTION  fun_Date_Friendly (@minutes int)
RETURNS nvarchar(100)
AS
BEGIN
   return CASE 
    when @minutes < 60 then cast( @minutes as varchar(10)) + ' Min'
    when @minutes < 1440 then cast(@minutes/60 as varchar(10)) + ' Hr, ' + cast(@minutes%60 as varchar(10)) + ' Min'
    else cast(@minutes/(1440 ) as varchar(10)) + ' Days, ' + cast((@minutes%1440 )/60 as varchar(10)) + ' Hr, ' + cast(((@minutes%1440 )%60) as varchar(10)) + ' Min'
    end
end
go

然后只需将分钟传递给功能

select  dbo.fun_Date_Friendly(20) val