SQL Server 2008将int转换为dd:hh:mm

时间:2013-03-12 15:11:52

标签: datetime sql-server-2008-r2 int

如何将表示分钟的int值(即:1800)转换为如下所示的值:dd:hh:mmdays:hours:minutes)。

因此1800应转换为1:06:001 day 6 hours 0 minutes)。

在存储过程中,我有:

SELECT 
    Record_ID, Project_ID, Ticket_ID, WO_Type, DC, Title, Device_Quantity, 
    Total/1440 as Total,  
    ((Total - Elapsed) - DATEDIFF(mi, Record_Time, getdate())) as FinalTimeLeft, 
    Completed 
FROM Record

我如何在SP中实施投射?高于FinalTimeLeft=1800

1 个答案:

答案 0 :(得分:5)

这个例子怎么样 - 它可以显示小时,分钟和秒,但是应该很容易修改几天,几小时和几分钟:

SELECT 
  CAST(mins / 3600 AS VARCHAR) + ':' + 
  RIGHT('0' + CAST((mins % 3600) / 60  AS VARCHAR), 2) + ':' +
  RIGHT('0' + CAST(mins % 60 AS VARCHAR), 2)
FROM 
  (SELECT 1800 AS mins) a

编辑:包含您的存储过程,我的代码修改了日,小时和分钟:

SELECT 
  *
  ,CAST(FinalTimeLeft / 1440 AS VARCHAR) + ':' +
     RIGHT('0' + CAST((FinalTimeLeft / 60) % 24 AS VARCHAR), 2) + ':' +
     RIGHT('0' + CAST(FinalTimeLeft % 60 AS VARCHAR), 2) AS duration
FROM ( 
SELECT 
  Record_ID
  ,Project_ID
  ,Ticket_ID
  ,WO_Type
  ,DC
  ,Title
  ,Device_Quantity
  ,Total/1440 as Total
  ,((Total-Elapsed)-DATEDIFF(mi,Record_Time,getdate())) as FinalTimeLeft
  ,Completed
FROM record) a