sql查询日期格式(小时:分钟)

时间:2015-06-19 13:29:20

标签: sql sql-server-2008-r2 date-formatting qsqlquery

如何将值显示为小时:分钟格式(eg 77, Result: 1:17)

info:

select 
    SUM([Travel Time] + [Total Productive Time])  
from 
    vAction_Reg 
where 
    incident_id = '10064068'

结果:77.00

需要以下面的格式获得结果

(1:17)

更改了查询:

SELECT( SELECT ISNULL(SUM(action.service_time), 0) AS Expr1
    FROM dbo.act_reg AS action 
    INNER JOIN dbo.act_type ON action.act_type_id = dbo.act_type.act_type_id
    WHERE (dbo.act_type.act_type_n IN ('Travel Time')) AND (action.incident_id = inc.incident_id)) AS [Travel Time],
   ( SELECT  ISNULL(SUM(action.service_time), 0) AS Expr1
     FROM  dbo.act_reg AS action 
     INNER JOIN dbo.act_type AS act_type_6 ON action.act_type_id = act_type_6.act_type_id
     WHERE (act_type_6.act_type_n NOT IN ('Travel Time', 'Site Departure')) AND (action.incident_id = inc.incident_id)) AS [Total Productive Time],
     ( SELECT cast(total / 60 as varchar(8)) + ':' + cast(total % 60 as varchar(2)) 
       FROM 
       ( SELECT cast(sum([Travel Time] + [Total Productive Time]) as int) as total) T) AS [Total Service Time]                        
         FROM dbo.incident AS inc 
         INNER JOIN dbo.assyst_usr ON inc.ass_usr_id = dbo.assyst_usr.assyst_usr_id 
         INNER JOIN dbo.serv_dept ON dbo.assyst_usr.serv_dept_id = dbo.serv_dept.serv_dept_id  
         WHERE (inc.incident_ref ='64483')

2 个答案:

答案 0 :(得分:3)

你可以分为几小时,mod为剩余分钟:

select 
cast(fld / 60 as varchar(8)) + ':' + cast(fld % 60 as varchar(2))

?money?的更新 - 假设没有小数部分;

select 
    cast(total / 60 as varchar(8)) + ':' + cast(total % 60 as varchar(2)) 
from (
    select 
        cast(sum([Travel Time] + [Total Productive Time]) as int) as total
    from 
        vAction_Reg 
    where 
        incident_id = '10064068'
) T

答案 1 :(得分:0)

创建如下函数:

CREATE FUNCTION [dbo].[NumberToTime]
(  
    @Num int
)
RETURNS nvarchar(20)
AS  
BEGIN
DECLARE @TimeDuration AS nvarchar(20)
SET @TimeDuration = (SELECT CAST((@Num / 60) AS VARCHAR(8)) + ':' + 
                CAST((@Num % 60) AS VARCHAR(2)))
RETURN @TimeDuration
END

使用该功能:

select 
dbo.NumberToTime(SUM([Travel Time] + [Total Productive Time])) from 
vAction_Reg where 
incident_id = '10064068'

它将返回:1:17