Results 1 to 5 of 5
  1. #1
    Join Date
    Aug 2004
    Posts
    2

    Unanswered: Date diff in SQL-Query

    Hello,

    i'm trying to get the difference between 2 datetime fields, but MySQL seems to work wrong on this. In my case i'd like to get the difference between 08:59am and 09:01am for example. I tried it within a query like "select sum(stop-start) as diff from times where ..." but this doesnt work and the result is more than i expected. Does anybody know how to substitude one datetime field from another.

    Thx
    Begar

  2. #2
    Join Date
    Feb 2004
    Location
    In front of the computer
    Posts
    15,579
    Provided Answers: 54
    I'd use the TIMESTAMPDIFF function.

    -PatP

  3. #3
    Join Date
    Apr 2002
    Location
    Toronto, Canada
    Posts
    20,002
    Quote Originally Posted by Pat Phelan
    I'd use the TIMESTAMPDIFF function.
    you would

    but i wouldn't, because it's only in version 5.0

    last time i looked, 4.1 was still in beta, and 5.0 was in alpha

    i.e. very unreliable

    instead, i would calculate the difference in seconds like this:

    select UNIX_TIMESTAMP(stop)-UNIX_TIMESTAMP(start)

    then i might use FROM_UNIXTIME() if i knew the difference was gonna be more than a day, and maybe SEC_TO_TIME() if less
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL

  4. #4
    Join Date
    Aug 2004
    Posts
    2
    but in my case UNIX_TIMESTAMP of my Datetime field is always 0. Does anybody have an Idea why ist is always 0?

    Another Problem is, that i m working with Access and when i m using Mysql-functions i get an error that Access cant work with these functions.

    Any Idea?
    Begar

  5. #5
    Join Date
    Apr 2002
    Location
    Toronto, Canada
    Posts
    20,002
    Quote Originally Posted by Begar
    but in my case UNIX_TIMESTAMP of my Datetime field is always 0. Does anybody have an Idea why ist is always 0?
    possibly because it's null, possibly because it's not a valid datetime value

    Another Problem is, that i m working with Access and when i m using Mysql-functions i get an error that Access cant work with these functions.
    the reason for this is because access does not support mysql functions
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL

Posting Permissions

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