Difference of two date time in sql server

后端 未结 20 1940
心在旅途
心在旅途 2020-12-01 03:53

Is there any way to take the difference between two datetime in sql server?

For example, my dates are

  1. 2010-01-22 15:29:55.090
相关标签:
20条回答
  • 2020-12-01 04:45

    I tried this way and it worked. I used SQL Server version 2016

    SELECT DATEDIFF(MILLISECOND,'2010-01-22 15:29:55.090', '2010-01-22 15:30:09.153')/1000.00;
    

    Different DATEDIFF Functions are:

    SELECT DATEDIFF(year,        '2005-12-31 23:59:59.9999999', '2006-01-01 00:00:00.0000000');
    SELECT DATEDIFF(quarter,     '2005-12-31 23:59:59.9999999', '2006-01-01 00:00:00.0000000');
    SELECT DATEDIFF(month,       '2005-12-31 23:59:59.9999999', '2006-01-01 00:00:00.0000000');
    SELECT DATEDIFF(dayofyear,   '2005-12-31 23:59:59.9999999', '2006-01-01 00:00:00.0000000');
    SELECT DATEDIFF(day,         '2005-12-31 23:59:59.9999999', '2006-01-01 00:00:00.0000000');
    SELECT DATEDIFF(week,        '2005-12-31 23:59:59.9999999', '2006-01-01 00:00:00.0000000');
    SELECT DATEDIFF(hour,        '2005-12-31 23:59:59.9999999', '2006-01-01 00:00:00.0000000');
    SELECT DATEDIFF(minute,      '2005-12-31 23:59:59.9999999', '2006-01-01 00:00:00.0000000');
    SELECT DATEDIFF(second,      '2005-12-31 23:59:59.9999999', '2006-01-01 00:00:00.0000000');
    SELECT DATEDIFF(millisecond, '2005-12-31 23:59:59.9999999', '2006-01-01 00:00:00.0000000');
    

    Ref: https://docs.microsoft.com/en-us/sql/t-sql/functions/datediff-transact-sql?view=sql-server-2017

    0 讨论(0)
  • 2020-12-01 04:47
    SELECT DATEDIFF(yyyy, '2011/08/25', '2017/08/25') AS DateDiff
    

    It's gives you difference between two dates in Year

    Here (2017-2011)=6 as a result

    Syntax:

    DATEDIFF(interval, date1, date2)
    
    0 讨论(0)
  • 2020-12-01 04:48

    I can mention four important functions of MS SQL Server that can be very useful:

    1) The function DATEDIFF() is responsible to calculate differences between two dates, the result could be "year quarter month dayofyear day week hour minute second millisecond microsecond nanosecond", specified on the first parameter (datepart):

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

    2) You can use the function GETDATE() to get the actual time and calculate differences of some date and actual date:

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

    3) Another important function is DATEADD(), used to convert some value in datetime using the same datepart of the datediff, that you can add (with positive values) or substract (with negative values) to one base 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) The function CONVERT() was made to format the date like you need, it is not parametric function, but you can use part of the result to format the result like you need:

    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 cold be calculated in seconds and one interesting result mixing these four function is to show a formated difference um hours, minutes and seconds (hh:mm:ss) between two dates:

    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)
    

    ... the result is 00:10:38 (638s = 600s + 38s = 10 minutes and 38 seconds)

    Another example:

    select distinct convert(char(8),dateadd(s,datediff(s, CRDATE , GETDATE() ),'1900-1-1'),8) from sysobjects order by 1
    
    0 讨论(0)
  • 2020-12-01 04:48

    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'
    

    END

    0 讨论(0)
  • 2020-12-01 04:53
    SELECT DATEDIFF (MyUnits, '2010-01-22 15:29:55.090', '2010-01-22 15:30:09.153')
    

    Substitute "MyUnits" based on DATEDIFF on MSDN

    0 讨论(0)
  • 2020-12-01 04:55

    Internally in SQL Server dates are stored as 2 integers. The first integer is the number of dates before or after the base date (1900/01/01). The second integer stores the number of clock ticks after midnight, each tick is 1/300 of a second.

    More info here

    Because of this, I often find the simplest way to compare dates is to simply substract them. This handles 90% of my use cases. E.g.,

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

    When I need an answer in units other than days, I will use DateDiff.

    0 讨论(0)
提交回复
热议问题