Results 1 to 5 of 5

Thread: DateDiff

  1. #1
    Join Date
    May 2004
    Posts
    36

    Unanswered: DateDiff

    I'm using a datediff(mi, start, stop) to get the duration of an operation. i want it displayed in HH:MM format. can anyone help me w/ a way to do that????



    tia,
    e3witt

  2. #2
    Join Date
    Feb 2004
    Location
    In front of the computer
    Posts
    15,579
    Provided Answers: 54
    I'd use:
    Code:
    SELECT Convert(CHAR(5), DateAdd(min
    ,  DateDiff(min, start, stop), '00:00:00')
    ,  108)
    -PatP

  3. #3
    Join Date
    May 2004
    Posts
    36
    thanks! that works like a charm.....

  4. #4
    Join Date
    Nov 2002
    Location
    Jersey
    Posts
    10,322
    Quote Originally Posted by Pat Phelan
    I'd use:
    Code:
    SELECT Convert(CHAR(5), DateAdd(min
    ,  DateDiff(min, start, stop), '00:00:00')
    ,  108)
    -PatP

    [shameless post]


    Thank GOD!

    Someone is taking all the difficult questions...

    Where would we be otherwise?

    OH that's right x002548 is awol

    Now will somone PLEASE fix me a 'rita

    [/shameless post]
    Brett
    8-)

    It's a Great Day for America everybody!

    dbforums Yak CorralRadio 'Rita
    dbForums Member List
    I'm Good Once as I ever was

    The physical order of data in a database has no meaning.

  5. #5
    Join Date
    Jul 2003
    Location
    San Antonio, TX
    Posts
    3,662
    This is the code PaulYoung posted some time ago, as as such became public domain.
    Code:
    if object_id('dbo.fn_ElapsedTime') is not null
       drop function dbo.fn_ElapsedTime
    go
    /*
       ****************************************************************
       Created :   06/01/2004
       By	  :   Paul Young (dbForums participant)
       Purpose :   Return elapsed time in 99:99:99.999 format.
       Execute :   select dbo.fn_ElapsedTime(@date_start, @date_end)
       ****************************************************************
    */
    create function dbo.fn_ElapsedTime (
       @starttime datetime, 
       @endtime datetime = Null) returns varchar(40)
    as begin
       declare @d	int, 
    		   @h	int, 
    		   @m	int, 
    		   @s	int, 
    		   @ms   int, 
    		   @dif1 int, 
    		   @ret  varchar(40)
       select  @d  = 0, 
    		   @h  = 0, 
    		   @m  = 0, 
    		   @s  = 0, 
    		   @ms = 0
       set @d = datediff(dd,@starttime,@endtime)
       set @dif1 = datediff(ms,dateadd(dd,@d,@starttime),@endtime)
       
       if (@dif1 > 0) begin
    	  set @ms = @dif1 % 1000
    	  set @dif1 = @dif1 - @ms
    	  set @s = ((@dif1 / 1000) % 60)
    	  set @dif1 = @dif1 - (@s * 1000)
    	  set @m = ((@dif1 / 60000) % 60)
    	  set @dif1 = @dif1 - (@m * 60000)
    	  set @h = ((@dif1 / 3600000) % 60)
       end
       
       set @ret = cast(@d as varchar(25)) + ':' +
    	  right('00' + cast(@h as varchar(2)),2) + ':' +
    	  right('00' + cast(@m as varchar(2)),2) + ':' + 
    	  right('00' + cast(@s as varchar(2)),2) + '.' + 
    	  right('000' + cast(@ms as varchar(3)),3)
       
       return @ret
    end
    
    "The data in a record depends on the Key to the record, the Whole Key, and
    nothing but the Key, so help me Codd."

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •