如何在此过程中将小数小时转换为小时和分钟

时间:2015-05-22 09:35:00

标签: sql sql-server stored-procedures

我有一个存储过程,返回4列数据

  • Supporthoursworked(这是以分钟为单位返回并乘以60)
  • SupportHoursCharged
  • DevelopmentHoursWorked(也以分钟为单位返回并乘以)
  • DevelopmentHoursCharged

我的问题是它目前以十进制形式返回这些值,并且很难将这些值转换为标准时间,

如何修改此程序以正确返回时间?

USE [Database]
GO
/****** Object:  StoredProcedure [dbo].[usp_JobTimeSystem_FetchDepartment]    Script Date: 22/05/2015 10:33:23 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

ALTER PROCEDURE [dbo].[usp_JobTimeSystem_FetchDepartment]

@FromDate                           datetime,
@ToDate                             datetime,
@SystemUserID                       uniqueidentifier

AS

;WITH cte AS (

SELECT
       DATEPART(Year, StartTime) AS YearNumber,
       DATEPART(Month, StartTime) AS MonthNumber,
       DateName(Month, StartTime) + ' ' + CAST(DatePart(Year, StartTime) AS nvarchar(50)) AS TimePeriod,
       DATEADD(day, DATEDIFF(Day, 0, StartTime), 0) AS FromDate,
       isnull(DATEDIFF(minute, StartTime, EndTime), 0.0) AS JobTime,
       isnull(tblJobWorkLog.ChargeableTime, 0.0) AS ChargeableTime,
       WorkLogJobTypeID,
       tblJobWorkLog.SystemUserID

FROM
       tblJobWorkLog
       INNER JOIN tblJob ON tblJobWorkLog.JobID = tblJob.JobID
       INNER JOIN tblContact ON tblJob.ContactID = tblContact.ContactID

WHERE
       tblJobWorkLog.StartTime >= @FromDate
       AND tblJobWorkLog.EndTime <= @ToDate
       AND SystemUserID = @SystemUserID

)

Select 
      FromDate
    , SUM(SupportHoursWorked)       AS SupportHoursWorked
    , SUM(SupportHoursCharged)      AS SupportHoursCharged
    , SUM(DevelopmentHoursWorked)   AS DevelopmentHoursWorked
    , SUM(DevelopmentHoursCharged)  AS DevelopmentHoursCharged

From (

    SELECT
             FromDate,
           Case when 
                WorkLogJobTypeID = 'FA5E6979-D228-44B7-A91B-8DDC8DDC709B'       -- SUPPORT
                OR WorkLogJobTypeID = '3171B295-60E9-4724-95A3-04FA182D7D43'    -- QUOTE
                OR WorkLogJobTypeID = '52c2691f-ff0a-4263-a440-8a309f868f93'    -- HARDWARE
                then (SUM(JobTime) / 60.0) else 0.0 end as SupportHoursWorked,

            Case when
                WorkLogJobTypeID = 'FA5E6979-D228-44B7-A91B-8DDC8DDC709B'       -- SUPPORT
                OR WorkLogJobTypeID = '3171B295-60E9-4724-95A3-04FA182D7D43'    -- QUOTE
                OR WorkLogJobTypeID = '52c2691f-ff0a-4263-a440-8a309f868f93'    -- HARDWARE
                then (SUM(ChargeableTime)) else 0.0 end AS SupportHoursCharged,

            Case when
                 WorkLogJobTypeID = 'D0E910B1-B4BD-430C-AD04-EB4E67946806'      -- DATA
                 OR WorkLogJobTypeID = 'B0BBF362-294D-4262-BED8-EDA7EE74745B'   -- EMAILS
                 OR WorkLogJobTypeID = '1E333ADC-E4F2-4042-8B65-E25F2770D59F'   -- WEB DEV
                 OR WorkLogJobTypeID = 'A445B7CE-E9E4-48E6-B5AA-83C83F045315'   -- WEB SUPP
                 OR WorkLogJobTypeID = '1D83F510-87FA-446E-9337-3D0376210D57'   -- SOFTWARE DEV
                 OR WorkLogJobTypeID = 'B59C1596-E1D0-4118-A805-65208E27AFB5'   -- SOFTWARE SUPP
                 OR WorkLogJobTypeID = 'F44A4B3C-B149-45A8-A9F0-5A57883482FD'   -- EMAILS
                 then (SUM(JobTime) / 60.0) else 0.0 end as DevelopmentHoursWorked,

            Case when
                 WorkLogJobTypeID = 'D0E910B1-B4BD-430C-AD04-EB4E67946806'      -- DATA
                 OR WorkLogJobTypeID = 'B0BBF362-294D-4262-BED8-EDA7EE74745B'   -- EMAILS
                 OR WorkLogJobTypeID = '1E333ADC-E4F2-4042-8B65-E25F2770D59F'   -- WEB DEV
                 OR WorkLogJobTypeID = 'A445B7CE-E9E4-48E6-B5AA-83C83F045315'   -- WEB SUPP
                 OR WorkLogJobTypeID = '1D83F510-87FA-446E-9337-3D0376210D57'   -- SOFTWARE DEV
                 OR WorkLogJobTypeID = 'B59C1596-E1D0-4118-A805-65208E27AFB5'   -- SOFTWARE SUPP
             OR WorkLogJobTypeID = 'F44A4B3C-B149-45A8-A9F0-5A57883482FD'   -- EMAILS
            then SUM(ChargeableTime) else 0.0 end as DevelopmentHoursCharged

            FROM
       cte

GROUP BY
       FromDate, WorkLogJobTypeID

) a -- CTETableName

Group by FromDate

ORDER BY
       FromDate

全部谢谢

3 个答案:

答案 0 :(得分:0)

如果您希望它作为标准日期时间,您可以使用以下表达式:

dateadd(mi,SUM(SupportHoursWorked)*60,'00:00')  

如果你想要它“美丽”:

select CONVERT(VARCHAR(8), dateadd(mi,SUM(SupportHoursWorked)*60,'00:00'), 108) 'hh:mi:ss'

答案 1 :(得分:0)

对于Sql Server 2012及以上:

SELECT timefromparts(SUM(SupportHoursWorked), 0, 0, 0, 0)

一般:

SELECT DATEADD(hh, SUM(SupportHoursWorked), CAST('00:00:00' AS TIME))

修改

替换:

Select 
  FromDate
, SUM(SupportHoursWorked)       AS SupportHoursWorked
, SUM(SupportHoursCharged)      AS SupportHoursCharged
, SUM(DevelopmentHoursWorked)   AS DevelopmentHoursWorked
, SUM(DevelopmentHoursCharged)  AS DevelopmentHoursCharged

使用:

Select 
  FromDate
, DATEADD(mi, SUM(SupportHoursWorked)*60 , CAST('00:00:00' AS TIME))      AS SupportHoursWorked
, DATEADD(mi, SUM(SupportHoursCharged)*60, CAST('00:00:00' AS TIME))      AS SupportHoursCharged
, DATEADD(mi, SUM(DevelopmentHoursWorked)*60, CAST('00:00:00' AS TIME))   AS DevelopmentHoursWorked
, DATEADD(mi, SUM(DevelopmentHoursCharged)*60, CAST('00:00:00' AS TIME))  AS DevelopmentHoursCharged

答案 2 :(得分:0)

我认为这是一个简单的答案。尝试将它们转换为Time数据类型。时间打印出像hh:mm:ss [.nnnnnnn]