Results 1 to 6 of 6

Thread: MSSQL - Time ?

  1. #1
    Join Date
    Aug 2004
    Posts
    19

    Question MSSQL - Time ?

    Based on the tables and data below,
    how can I calculate the amount of time
    between the two columns in hours/secs?

    TimeIn [datetime]
    2004-08-17 15:52:38.000

    TimeOut[datetime]
    2004-08-17 17:54:02.000


    Thanks

  2. #2
    Join Date
    Feb 2003
    Posts
    1,048
    Use the DateDiff function.

  3. #3
    Join Date
    Sep 2002
    Location
    Fantasy
    Posts
    4,254
    create table timetest1 (TimeIn datetime,TimeOut datetime)
    insert into timetest1 select '2004-08-17 15:52:38.000' , '2004-08-17 17:54:02.000'
    insert into timetest1 select '2004-08-13 15:52:38.000' , '2004-08-17 17:54:02.000'
    insert into timetest1 select '2004-07-17 15:52:38.000' , '2004-08-17 17:54:02.000'
    insert into timetest1 select '2002-07-17 15:52:38.000' , '2004-08-17 17:54:02.000'

    select timeout-timein from timetest1

  4. #4
    Join Date
    Aug 2004
    Posts
    19

    Question

    Im still not sure what to do here.
    This gives me what I want but I do not know how to put this result set into a table or view of its own without using a cursor. Any Ideas?

    DECLARE AddHours CURSOR FOR
    Select (datediff(hh,TimeIn,TimeOut))
    as HrsWorked from TimeTest1
    OPEN AddHours
    FETCH NEXT FROM AddHours
    WHILE @@FETCH_STATUS = 0
    FETCH NEXT FROM AddHours
    CLOSE AddHours
    DEALLOCATE AddHours

  5. #5
    Join Date
    Feb 2003
    Posts
    1,048
    This is what I was suggesting you use:

    Create Table #timetest1 (TimeIn datetime, TimeOut datetime)

    Insert Into #timetest1
    Values ('2004-08-17 15:52:38.000 ', '2004-08-17 17:54:02.000')

    Select DateDiff(hh, TimeIn, TimeOut) As DifferenceInHours,
    DateDiff(ss, TimeIn, TimeOut) As DifferenceInSeconds,
    DateDiff(ss, TimeIn, TimeOut)%(60 * 60) As LeftOverSeconds
    From #timetest1

    Drop Table #timetest1

  6. #6
    Join Date
    Aug 2004
    Posts
    19

    Thumbs up

    That is what I needed, thanks so much.

Posting Permissions

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