T-SQL中没有TimeDiff功能?

时间:2013-06-10 16:20:59

标签: sql-server-2008 user-defined-functions

我创建了一个名为DesiredTimeOfFileCreation的新列,类型为time(7);这将指示将数据提取到导出文件的时间。

我们假设它设置为6:00:00。然后,我在6:00(可能每30分钟)安排一个SQL代理作业,但它可能在6:00:05甚至6:01运行。我想选择DesiredTimeOfFileCreation小于30分钟前的所有行。

有人已经拥有用户定义的TimeDiff功能吗?还是有一个我想念的简单替代品?

2 个答案:

答案 0 :(得分:1)

正如马丁上面提到的,我需要处理午夜环绕。

这似乎过于复杂。如果一个时间是午夜前一小时,一个是在一个小时之后,下面的代码似乎有用。很高兴让它更通用。我认为唯一的方法就是组成一个虚拟日期,我可以在下面进行试验。

我在单元测试中传递日期的原因是我将把GetUTCDate()的演出版本作为parm传递:

ALTER FUNCTION TimeDiffMinutes
(
    @FirstTime time(7), 
    @SecondTime time(7)

)
RETURNS int 
AS
BEGIN

/* 

Unit Test: 
   select dbo.TimeDiffMinutes('13:31',cast ('2013-06-10 13:35' as time)),  -- simple test 
          dbo.TimeDiffMinutes('23:55',cast ('2013-06-10 00:05' as time))   -- test midnight wrap-around 

   select dbo.TimeDiffMinutes('23:55',cast ('2013-06-10 00:05' as time))   -- test midnight wrap-around 

*/ 

    -- Declare the return variable here
    DECLARE @resultMinutes int 
    DECLARE @Hour int 

    -- although we can compare two times, the problem is that if one time is 11:55 and the other is 00:05, we want to show 10 minutes difference. 
    -- We cannot add 24 hours to a time, because that would be an invalid value 

    Set @Hour = datePart(hour,@SecondTime) 

    if (@Hour <= 0) 
      begin 
         -- increase both times by an hour so we can compare them, 23:55 will wrap around to 01:55
         Set @FirstTime = DateAdd(hour,+1,@FirstTime)
         Set @SecondTime = DateAdd(hour,+1,@SecondTime)
      end 

    SET @resultMinutes = DATEDIFF(Minute,@FirstTime,@SecondTime)                   


    -- Return the result of the function
    RETURN @resultMinutes 


END

注意:此代码显示您不能同时超过24小时;它只是回绕(没有错误!):

declare @FirstTime time(7)
SET @FirstTime = '23:05' 
print @FirstTime
Set @FirstTime = DATEADD(HOUR,1,@FirstTime)
print @FirstTime

改进版本,使用任意日期。

ALTER FUNCTION TimeDiffMinutes
(
    @FirstTime time(7), 
    @SecondTime time(7)

)
RETURNS int 
AS
BEGIN

/* 

Unit Test: 
   select dbo.TimeDiffMinutes('13:31',cast ('2013-06-10 13:35' as time)),  -- simple test 
          dbo.TimeDiffMinutes('23:55',cast ('2013-06-10 00:05' as time))   -- test midnight wrap-around 

   select dbo.TimeDiffMinutes('23:55',cast ('2013-06-10 00:05' as time))   -- test midnight wrap-around 

*/ 

    -- Declare the return variable here
    DECLARE @resultMinutes int 
    DECLARE @Hour int 
    DECLARE @FirstDate datetime 
    DECLARE @SecondDate datetime

    Set @FirstDate  = CAST('2001-01-01 ' + Convert(varchar(12),@FirstTime) as DateTime)
    Set @SecondDate = CAST('2001-01-01 ' + Convert(varchar(12),@SecondTime) as DateTime)


    -- although we can compare two times, the problem is that if one time is 11:55 and the other is 00:05, we want to show 10 minutes difference. 
    -- We cannot add 24 hours to a time, because that would be an invalid value 

    Set @Hour = datePart(hour,@SecondDate) 

    if (@Hour <= 0) 
      begin 
         -- increase both times by an hour so we can compare them, 23:55 will wrap around to 01:55
         Set @SecondDate = DateAdd(day,+1,@SecondDate)
      end 

    SET @resultMinutes = DATEDIFF(Minute,@FirstDate,@SecondDate)                   


    -- Return the result of the function
    RETURN @resultMinutes 


END

这就是我将如何使用该功能。我们将机场想要提取文件的当地时间存储在表格中。然后我们将使用SQL代理或BizTalk每30分钟轮询一次,查找要做的工作。 AirportCode是表格中的一列,我们有自己的疯狂函数来转换时区。

select *, 
       dbo.TimeDiffMinutes(
                DesiredFileCreationTimeLocal,
                cast(dbo.LocationLocalTimeFromAirportCode(AirportCode,GETUTCDATE()) as time)
                ) as 'MinutesAgo'
from TransactionExtractDistribution 
where    dbo.TimeDiffMinutes(
                DesiredFileCreationTimeLocal,
                cast(dbo.LocationLocalTimeFromAirportCode(AirportCode,GETUTCDATE()) AS time)
                ) < 30 

答案 1 :(得分:0)

这可能对我有用:

 WHERE DATEDIFF(Minute,DesiredFileCreationTimeLocal,cast(GETDATE() as time)) < 30

如果将Time作为变量传递给DateDiff函数,我必须研究发生了什么。 它似乎工作,唯一的技巧是如何传递两次。

我的真实场景更复杂,因为我们正在处理不同时区的不同位置,因此会在上面添加一些UTC转换。