SQL中的数据格式化(将分钟转换为HH:MM格式)

时间:2015-06-20 12:01:36

标签: sql sql-server-2008

如何以HH:Min格式显示[总服务时间](目前以分钟为单位的值)?

查询:

SELECT  DISTINCT  dbo.sectn_dept.sectn_sc AS Customer,  MONTH(dbo.incident.date_logged) AS Month_Number, DATENAME(month,    dbo.incident.date_logged) AS Month, YEAR(dbo.incident.date_logged) AS Year, 
dbo.incident.incident_ref PM_ref,   dbo.product.product_n "Product",    dbo.item.item_n "Item",
dbo.bldng_room.bldng_room_sc,   dbo.assyst_usr.assyst_usr_n,    dbo.incident.last_action_date "Last Action",
Status=case dbo.incident.inc_status when 'c' then 'Closed' when 'o' then 'Open' else 'Resolved' end,
CASE dbo.act_reg.act_type_sc WHEN 'ADD_ATTCHMNTS' THEN 'Yes' ELSE 'No' END AS Attachmnet    ,
    (select    cast(total / 60 as varchar(8)) + ':' + cast(total % 60 as varchar(2)) 
    from (  select      cast(sum(AR.[Travel Time] + AR.[Total Productive Time]) as int) as total from      dbo.vAction_Reg as AR ) T) AS [Total Service Time]
FROM         dbo.bldng INNER JOIN
dbo.site ON dbo.bldng.site_id = dbo.site.site_id INNER JOIN
dbo.incident INNER JOIN dbo.item ON dbo.incident.item_id = dbo.item.item_id INNER JOIN
dbo.product ON dbo.item.product_id = dbo.product.product_id INNER JOIN
dbo.sectn_dept ON dbo.incident.sectn_dept_id = dbo.sectn_dept.sectn_dept_id INNER JOIN
dbo.inc_cat ON dbo.incident.inc_cat_id = dbo.inc_cat.inc_cat_id INNER JOIN
dbo.inc_major ON dbo.inc_cat.inc_major_id = dbo.inc_major.inc_major_id  INNER JOIN
dbo.bldng_room ON dbo.incident.bldng_room_id = dbo.bldng_room.bldng_room_id ON dbo.bldng.bldng_id = dbo.bldng_room.bldng_id INNER JOIN
dbo.prod_cls ON dbo.product.prod_cls_id = dbo.prod_cls.prod_cls_id      inner JOIN
dbo.assyst_usr ON dbo.incident.ass_usr_id = dbo.assyst_usr.assyst_usr_id    LEFT OUTER Join
dbo.vAction_Reg on dbo.incident.incident_id=dbo.vAction_Reg.incident_id         inner JOIN
dbo.act_reg ON dbo.incident.incident_id=dbo.act_reg.incident_id

WHERE     
dbo.inc_major.inc_major_sc = 'PM' AND   dbo.incident.date_logged >='6/19/2015'
GROUP BY    dbo.sectn_dept.sectn_sc ,   dbo.incident.date_logged,       dbo.incident.incident_ref,
dbo.product.product_n,      dbo.item.item_n,        dbo.bldng_room.bldng_room_sc,   dbo.assyst_usr.assyst_usr_n,
dbo.incident.inc_status,    dbo.act_reg.act_type_sc,    dbo.incident.last_action_date,
dbo.vAction_Reg.[Total Productive Time],dbo.vAction_Reg.[Travel Time]

结果:

Customer    Month   Month   Year    PM_ref  Product Item    bldng_room_sc   assyst_usr_n    Last Action Status  Attachmnet  Total Service Time
DIB 6   June    2015    64625   PC2150XE    123 ABC TRADING SM  Hardy Colar 20-06-15    Resolved    No  291193:46
DIB 6   June    2015    64629   PC2100XE    345 ABC TRADING SM  Hardy Colar 20-06-15    Resolved    No  291193:46
HSBC    6   June    2015    64615   PC2100XEUSB+CCDM    765 JUMEIRA BR-OPP SAFA PARK    Fawad Shakeel   19-06-15    Resolved    No  291193:46
HSBC    6   June    2015    64616   PC2100XE+CCDM   787 JUMEIRA BR-OPP SAFA PARK    Fawad Shakeel   19-06-15    Resolved    No  291193:46
UNB 6   June    2015    64626   C2070RL 7627    DHAFRA-BRANCH   Hardy Colar 20-06-15    Resolved    No  291193:46

需要以下列格式显示总服务时间:

Total Service Time
1:02
1:51
1:03
1:02
3:31

实际表数据(Dtata type = Money)

Travel Time Total Productive Time
1.00    61.00
60.00   51.00
2.00    61.00
1.00    61.00
180.00  31.00

2 个答案:

答案 0 :(得分:0)

我认为最简单的方法就是进行字符串操作:

For i = 1 To Cells(Cells.Rows.Count, 2).End(xlUp).Row

答案 1 :(得分:0)

如果您使用的是SQL Server,可以使用CONVERT轻松完成更改为所需的样式:

SELECT CONVERT(varchar(5), DATEADD(ms, 120 * 1000, 0), 108)

乘以1000以更改为毫秒以丢弃小数部分

输出将是:

00:02