sql server中两个date时间的区别

有没有办法在SQL Server中的两个datetime时间之间的区别?

例如,我的date是

  1. 2010-01-22 15:29:55.090
  2. 2010-01-22 15:30:09.153

所以结果应该是14.063 seconds

只需要添加有关DateDiff的警告信息,它会计算您通过指定为您的单位的边界的次数,如果您正在寻找精确的时间间隔,则会出现问题。 例如

 select datediff (m, '20100131', '20100201') 

给出1的答案,因为它在1月到2月之间越过边界,所以尽pipe跨度为2天,但是如果返回值为1,则它跨越了1个边界。

 select datediff(mi, '2010-01-22 15:29:55.090' , '2010-01-22 15:30:09.153') 

给出的值为1,再次通过一次分钟边界,所以即使是大约14秒,当使用分钟作为单位时,它将作为一分钟返回。

 SELECT DATEDIFF (MyUnits, '2010-01-22 15:29:55.090', '2010-01-22 15:30:09.153') 

在MSDN上根据DATEDIFFreplace“MyUnits”

 SELECT DATEDIFF(day, '2010-01-22 15:29:55.090', '2010-01-22 15:30:09.153') 

将其他单位replace为您希望得到的差异,如secondminute

我可以提到MS SQL Server的四个重要function,可以是非常有用的:

1)函数DATEDIFF()负责计算两个date之间的差异,结果可以是“第一个参数( date部分 )上指定的” 年季度一天每一小时一小时每分钟毫秒微秒纳秒 “:

 select datediff(day,'1997-10-07','2011-09-11') 

2)您可以使用函数GETDATE()来获取实际时间并计算某个date和实际date的差异:

 select datediff(day,'1997-10-07', getdate() ) 

3)另一个重要的函数是DATEADD() ,用于使用与datediff相同的datepart转换datetime中的某个值,您可以将其添加(具有正值)或减值(具有负值)到一个基准date:

 select DATEADD(day, 45, getdate()) -- actual datetime adding 45 days select DATEADD( s,-638, getdate()) -- actual datetime subtracting 10 minutes and 38 seconds 

4)函数CONVERT()可以根据需要设置date格式,它不是参数化函数,但是您可以使用部分结果来格式化结果,如:

 select convert( char(8), getdate() , 8) -- part hh:mm:ss of actual datetime select convert( varchar, getdate() , 112) -- yyyymmdd select convert( char(10), getdate() , 20) -- yyyy-mm-dd limited by 10 characters 

DATETIME冷时间以秒为单位计算,一个有趣的结果混合这四个函数是显示两个date之间的时差,分钟和秒( hh:mm:ss

 declare @date1 datetime, @date2 datetime set @date1=DATEADD(s,-638,getdate()) set @date2=GETDATE() select convert(char(8),dateadd(s,datediff(s,@date1,@date2),'1900-1-1'),8) 

…结果是00:10:38 (638s = 600s + 38s = 10分38秒)

另一个例子:

 select distinct convert(char(8),dateadd(s,datediff(s, CRDATE , GETDATE() ),'1900-1-1'),8) from sysobjects order by 1 

在SQL Server内部,date存储为2个整数。 第一个整数是基准date(1900/01/01)之前或之后的date数。 第二个整数存储午夜之后的时钟滴答的数量,每个滴答是1/300秒。

更多信息在这里

正因为如此,我经常发现比较date的最简单的方法就是简单地减去它们。 这处理我的用例的90%。 例如,

 select date1, date2, date2 - date1 as DifferenceInDays from MyTable ... 

当我需要天以外的单位的答案时,我会使用DateDiff 。

有很多方法可以查看date差异,而在比较date/时间时有更多方法。 这是我用来得到格式为“HH:MM:SS”的两个date之间的区别:

 ElapsedTime AS RIGHT('0' + CAST(DATEDIFF(S, StartDate, EndDate) / 3600 AS VARCHAR(2)), 2) + ':' + RIGHT('0' + CAST(DATEDIFF(S, StartDate, EndDate) % 3600 / 60 AS VARCHAR(2)), 2) + ':' + RIGHT('0' + CAST(DATEDIFF(S, StartDate, EndDate) % 60 AS VARCHAR(2)), 2) 

我用这个计算列,但你可以平凡地重写它作为一个UDF或查询计算。 请注意,这个逻辑舍入小数秒; 00:00.00到00:00.999被视为零秒,并显示为“00:00:00”。

如果您预计这段时间可能会超过几天,则此代码将在需要时切换到D:HH:MM:SS格式:

 ElapsedTime AS CASE WHEN DATEDIFF(S, StartDate, EndDate) >= 359999 THEN CAST(DATEDIFF(S, StartDate, EndDate) / 86400 AS VARCHAR(7)) + ':' + RIGHT('0' + CAST(DATEDIFF(S, StartDate, EndDate) % 86400 / 3600 AS VARCHAR(2)), 2) + ':' + RIGHT('0' + CAST(DATEDIFF(S, StartDate, EndDate) % 3600 / 60 AS VARCHAR(2)), 2) + ':' + RIGHT('0' + CAST(DATEDIFF(S, StartDate, EndDate) % 60 AS VARCHAR(2)), 2) ELSE RIGHT('0' + CAST(DATEDIFF(S, StartDate, EndDate) / 3600 AS VARCHAR(2)), 2) + ':' + RIGHT('0' + CAST(DATEDIFF(S, StartDate, EndDate) % 3600 / 60 AS VARCHAR(2)), 2) + ':' + RIGHT('0' + CAST(DATEDIFF(S, StartDate, EndDate) % 60 AS VARCHAR(2)), 2) END 

下面的查询应该给你正在寻找的确切的东西。

 select datediff(second, '2010-01-22 15:29:55.090' , '2010-01-22 15:30:09.153') 

这里是从MSDN的链接,你可以做什么与datedifffunction。 https://msdn.microsoft.com/en-us/library/ms189794.aspx

在联机丛书上检查DateDiff。

所以这不是我的答案,但我只是在网上search这样的问题时才发现这个问题。 这个人build立了一个计算小时,分钟和秒的程序。 链接和代码:

 --Creating Function If OBJECT_ID('UFN_HourMinuteSecond') Is Not Null Drop Function dbo.UFN_HourMinuteSecond Go Exec( 'Create Function dbo.UFN_HourMinuteSecond ( @StartDateTime DateTime, @EndDateTime DateTime ) Returns Varchar(10) As Begin Declare @Seconds Int, @Minute Int, @Hour Int, @Elapsed Varchar(10) Select @Seconds = ABS(DateDiff(SECOND ,@StartDateTime,@EndDateTime)) If @Seconds >= 60 Begin select @Minute = @Seconds/60 select @Seconds = @Seconds%60 If @Minute >= 60 begin select @hour = @Minute/60 select @Minute = @Minute%60 end Else Goto Final End Final: Select @Hour = Isnull(@Hour,0), @Minute = IsNull(@Minute,0), @Seconds = IsNull(@Seconds,0) select @Elapsed = Cast(@Hour as Varchar) + '':'' + Cast(@Minute as Varchar) + '':'' + Cast(@Seconds as Varchar) Return (@Elapsed) End' ) 
 declare @dt1 datetime='2012/06/13 08:11:12', @dt2 datetime='2012/06/12 02:11:12' select CAST((@dt2-@dt1) as time(0)) 

PRINT DATEDIFF(第二,'2010-01-22 15:29:55.090','2010-01-22 15:30:09.153')

 select datediff(millisecond,'2010-01-22 15:29:55.090','2010-01-22 15:30:09.153') / 1000.0 as Secs result: Secs 14.063 

只是想我会提到它。

CREATE FUNCTION getDateDiffHours(@fdate AS datetime,@ tdate as datetime)RETURNS varchar(50)AS BEGIN DECLARE @cnt int DECLARE @cntDate datetime DECLARE @dayDiff int DECLARE @dayDiffWk int DECLARE @hrsDiff decimal(18)

 DECLARE @markerFDate datetime DECLARE @markerTDate datetime DECLARE @fTime int DECLARE @tTime int DECLARE @nfTime varchar(8) DECLARE @ntTime varchar(8) DECLARE @nfdate datetime DECLARE @ntdate datetime ------------------------------------- --DECLARE @fdate datetime --DECLARE @tdate datetime --SET @fdate = '2005-04-18 00:00:00.000' --SET @tdate = '2005-08-26 15:06:07.030' ------------------------------------- DECLARE @tempdate datetime --setting weekends SET @fdate = dbo.getVDate(@fdate) SET @tdate = dbo.getVDate(@tdate) --RETURN @fdate SET @fTime = datepart(hh,@fdate) SET @tTime = datepart(hh,@tdate) --RETURN @fTime if datediff(hour,@fdate, @tdate) <= 9 RETURN(convert(varchar(50),0) + ' Days ' + convert(varchar(50),datediff(hour,@fdate, @tdate))) + ' Hours' else --setting working hours SET @nfTime = dbo.getV00(convert(varchar(2),datepart(hh,@fdate))) + ':' +dbo.getV00(convert(varchar(2),datepart(mi,@fdate))) + ':'+ dbo.getV00(convert(varchar(2),datepart(ss,@fdate))) SET @ntTime = dbo.getV00(convert(varchar(2),datepart(hh,@tdate))) + ':' +dbo.getV00(convert(varchar(2),datepart(mi,@tdate))) + ':'+ dbo.getV00(convert(varchar(2),datepart(ss,@tdate))) IF @fTime > 17 begin set @nfTime = '17:00:00' end else begin IF @fTime < 8 set @nfTime = '08:00:00' end IF @tTime > 17 begin set @ntTime = '17:00:00' end else begin IF @tTime < 8 set @ntTime = '08:00:00' end -- used for working out whole days SET @nfdate = dateadd(day,1,@fdate) SET @ntdate = @tdate SET @nfdate = convert(varchar,datepart(yyyy,@nfdate)) + '-' + convert(varchar,datepart(mm,@nfdate)) + '-' + convert(varchar,datepart(dd,@nfdate)) SET @ntdate = convert(varchar,datepart(yyyy,@ntdate)) + '-' + convert(varchar,datepart(mm,@ntdate)) + '-' + convert(varchar,datepart(dd,@ntdate)) SET @cnt = 0 SET @dayDiff = 0 SET @cntDate = @nfdate SET @dayDiffWk = convert(decimal(18,2),@ntdate-@nfdate) --select @nfdate,@ntdate WHILE @cnt < @dayDiffWk BEGIN IF (NOT DATENAME(dw, @cntDate) = 'Saturday') AND (NOT DATENAME(dw, @cntDate) = 'Sunday') BEGIN SET @dayDiff = @dayDiff + 1 END SET @cntDate = dateadd(day,1,@cntDate) SET @cnt = @cnt + 1 END --SET @dayDiff = convert(decimal(18,2),@ntdate-@nfdate) --datediff(day,@nfdate,@ntdate) --SELECT @dayDiff set @fdate = convert(varchar,datepart(yyyy,@fdate)) + '-' + convert(varchar,datepart(mm,@fdate)) + '-' + convert(varchar,datepart(dd,@fdate)) + ' ' + @nfTime set @tdate = convert(varchar,datepart(yyyy,@tdate)) + '-' + convert(varchar,datepart(mm,@tdate)) + '-' + convert(varchar,datepart(dd,@tdate)) + ' ' + @ntTime set @markerFDate = convert(varchar,datepart(yyyy,@fdate)) + '-' + convert(varchar,datepart(mm,@fdate)) + '-' + convert(varchar,datepart(dd,@fdate)) + ' ' + '17:00:00' set @markerTDate = convert(varchar,datepart(yyyy,@tdate)) + '-' + convert(varchar,datepart(mm,@tdate)) + '-' + convert(varchar,datepart(dd,@tdate)) + ' ' + '08:00:00' --select @fdate,@tdate --select @markerFDate,@markerTDate set @hrsDiff = convert(decimal(18,2),datediff(hh,@fdate,@markerFDate)) --select @hrsDiff set @hrsDiff = @hrsDiff + convert(int,datediff(hh,@markerTDate,@tdate)) --select @fdate,@tdate IF convert(varchar,datepart(yyyy,@fdate)) + '-' + convert(varchar,datepart(mm,@fdate)) + '-' + convert(varchar,datepart(dd,@fdate)) = convert(varchar,datepart(yyyy,@tdate)) + '-' + convert(varchar,datepart(mm,@tdate)) + '-' + convert(varchar,datepart(dd,@tdate)) BEGIN --SET @hrsDiff = @hrsDiff - 9 Set @hrsdiff = datediff(hour,@fdate,@tdate) END --select FLOOR((@hrsDiff / 9)) IF (@hrsDiff / 9) > 0 BEGIN SET @dayDiff = @dayDiff + FLOOR(@hrsDiff / 9) SET @hrsDiff = @hrsDiff - FLOOR(@hrsDiff / 9)*9 END --select convert(varchar(50),@dayDiff) + ' Days ' + convert(varchar(50),@hrsDiff) + ' Hours' RETURN(convert(varchar(50),@dayDiff) + ' Days ' + convert(varchar(50),@hrsDiff)) + ' Hours' 

结束

溶胶- 1:

 select StartTime , EndTime , CONVERT(NVARCHAR,(EndTime-StartTime), 108) as TimeDiff from [YourTable] 

溶胶2:

 select StartTime , EndTime , DATEDIFF(hh, StartTime, EndTime) , DATEDIFF(mi, StartTime, EndTime) % 60 from [YourTable] 

溶胶- 3:

 select DATEPART(hour,[EndTime]-[StartTime]) , DATEPART(minute,[EndTime]-[StartTime]) from [YourTable] 

Datepart运作最好