Results 1 to 3 of 3

Thread: time diff code

  1. #1
    Join Date
    Jun 2003
    Location
    Australia
    Posts
    8

    Wink Unanswered: time diff code

    declare @d datetime
    set @d = dateadd(hh,-12,getdate())
    select @d
    , dateadd(hh,18,convert(varchar(10),@d,120))
    , datediff(hh,dateadd(hh,18,convert(varchar(10),@d,1
    20)),@d)

    The above code does not giving the minute part. can anybody help me please

  2. #2
    Join Date
    Apr 2003
    Location
    Israel
    Posts
    81
    Could you explain what result do you want to get ?
    --
    kukuk

  3. #3
    Join Date
    Feb 2002
    Location
    Houston, TX
    Posts
    809
    elams, that's because you are useing 'hh' in datediff, try using 'mm'.

    or you could use:

    Code:
    Create  function 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
    Paul Young
    (Knowledge is power! Get some!)

Posting Permissions

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