Close_time和Open_time之间的时间间隔

时间:2017-04-14 02:51:26

标签: sql datetime sql-server-2012 intervals

我在SQL中有一个问题。 我使用MS SQL SERVER 2012 我需要计算Close_time和Open_time之间的差异。 如何制作年 - 月 - 小时 - 分 - 秒的范围。 **例如:

Start_time : 2017:01.01 20:30:15
Close_time : 2018.02.05  22:58:35
Output: 0001:01:04 02:28:20**

我知道这不正确,因为它不是时间间隔。

Select Name,
Convert(VARCHAR,CAST(AVG(CAST(CAST(CLOSE_TIME as datetime) - CAST(OPEN_TIME  as datetime)as float)) as datetime), 120)as TM
From [dbo].[INCIDENTS]

2 个答案:

答案 0 :(得分:0)

您的代码建议使用SQL Server。你可以减去这些值:

SELECT CAST(CLOSE_TIME as datetime) - CAST(OPEN_TIME as datetime)

注意:您应该将这些字段存储为日期/时间值。如果你是,只需减去它们:

SELECT (CLOSE_TIME - OPEN_TIME)

答案 1 :(得分:0)

如果您对UDF持开放态度。这个表值函数是精确且高效的。

我添加了一个名为ELAPSED的字段,这是您想要的格式,但我还留下了年,月,日,小时,分钟和秒。如果需要,可以删除它们

示例

Declare @YourTable table (ID int,DateBeg datetime,DateEnd datetime)
Insert Into @YourTable values
 (1,'2017-01-01 20:30:15','2018-02-05 22:58:35')

Select A.*
      ,B.Elapsed
 From  @YourTable A
 Cross Apply [dbo].[udf-Date-Elapsed](A.DateBeg,A.DateEnd) B

<强>返回

ID  DateBeg                   DateEnd                     Elapsed
1   2017-01-01 20:30:15.000   2018-02-05 22:58:35.000     0001:01:04 02:28:20

感兴趣的UDF

CREATE FUNCTION [dbo].[udf-Date-Elapsed] (@D1 DateTime,@D2 DateTime)
Returns Table
Return (
    with cteBN(N)   as (Select 1 From (Values(1),(1),(1),(1),(1),(1),(1),(1),(1),(1)) N(N)),
         cteRN(R)   as (Select Row_Number() Over (Order By (Select NULL))-1 From cteBN a,cteBN b,cteBN c),
         cteYY(N,D) as (Select Max(R),Max(DateAdd(YY,R,@D1))From cteRN R Where DateAdd(YY,R,@D1)<=@D2),
         cteMM(N,D) as (Select Max(R),Max(DateAdd(MM,R,D))  From (Select Top 12 R From cteRN Order By 1) R, cteYY P Where DateAdd(MM,R,D)<=@D2),
         cteDD(N,D) as (Select Max(R),Max(DateAdd(DD,R,D))  From (Select Top 31 R From cteRN Order By 1) R, cteMM P Where DateAdd(DD,R,D)<=@D2),
         cteHH(N,D) as (Select Max(R),Max(DateAdd(HH,R,D))  From (Select Top 24 R From cteRN Order By 1) R, cteDD P Where DateAdd(HH,R,D)<=@D2),
         cteMI(N,D) as (Select Max(R),Max(DateAdd(MI,R,D))  From (Select Top 60 R From cteRN Order By 1) R, cteHH P Where DateAdd(MI,R,D)<=@D2),
         cteSS(N,D) as (Select Max(R),Max(DateAdd(SS,R,D))  From (Select Top 60 R From cteRN Order By 1) R, cteMI P Where DateAdd(SS,R,D)<=@D2)

    Select [Years]   = cteYY.N
          ,[Months]  = cteMM.N
          ,[Days]    = cteDD.N
          ,[Hours]   = cteHH.N
          ,[Minutes] = cteMI.N
          ,[Seconds] = cteSS.N
          ,[Elapsed] = Format(cteYY.N,'0000')+':'+Format(cteMM.N,'00')+':'+Format(cteDD.N,'00')+' '+Format(cteHH.N,'00')+':'+Format(cteMI.N,'00')+':'+Format(cteSS.N,'00')
     From  cteYY,cteMM,cteDD,cteHH,cteMI,cteSS
)
--Max 1000 years
--Select * from [dbo].[udf-Date-Elapsed] ('1991-09-12 21:00:00.000',GetDate())
--Select * from [dbo].[udf-Date-Elapsed] ('2017-01-01 20:30:15','2018-02-05 22:58:35')
  

编辑 - 对于标量值函数

示例

Select [dbo].[udf-Str-Elapsed] ('2017-01-01 20:30:15','2018-02-05 22:58:35')

<强>返回

0001:01:04 02:28:20

标量函数

CREATE FUNCTION [dbo].[udf-Str-Elapsed] (@D1 DateTime,@D2 DateTime)
Returns varchar(100)
AS 
Begin
    Declare @RetVal varchar(100)
    ;with cteBN(N)   as (Select 1 From (Values(1),(1),(1),(1),(1),(1),(1),(1),(1),(1)) N(N)),
          cteRN(R)   as (Select Row_Number() Over (Order By (Select NULL))-1 From cteBN a,cteBN b,cteBN c),
          cteYY(N,D) as (Select Max(R),Max(DateAdd(YY,R,@D1))From cteRN R Where DateAdd(YY,R,@D1)<=@D2),
          cteMM(N,D) as (Select Max(R),Max(DateAdd(MM,R,D))  From (Select Top 12 R From cteRN Order By 1) R, cteYY P Where DateAdd(MM,R,D)<=@D2),
          cteDD(N,D) as (Select Max(R),Max(DateAdd(DD,R,D))  From (Select Top 31 R From cteRN Order By 1) R, cteMM P Where DateAdd(DD,R,D)<=@D2),
          cteHH(N,D) as (Select Max(R),Max(DateAdd(HH,R,D))  From (Select Top 24 R From cteRN Order By 1) R, cteDD P Where DateAdd(HH,R,D)<=@D2),
          cteMI(N,D) as (Select Max(R),Max(DateAdd(MI,R,D))  From (Select Top 60 R From cteRN Order By 1) R, cteHH P Where DateAdd(MI,R,D)<=@D2),
          cteSS(N,D) as (Select Max(R),Max(DateAdd(SS,R,D))  From (Select Top 60 R From cteRN Order By 1) R, cteMI P Where DateAdd(SS,R,D)<=@D2)

    Select @RetVal = Format(cteYY.N,'0000')+':'+Format(cteMM.N,'00')+':'+Format(cteDD.N,'00')+' '+Format(cteHH.N,'00')+':'+Format(cteMI.N,'00')+':'+Format(cteSS.N,'00')
     From  cteYY,cteMM,cteDD,cteHH,cteMI,cteSS

    Return @RetVal

End