If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > MySQL > Date diff in SQL-Query

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 08-11-04, 11:45
Begar Begar is offline
Registered User
 
Join Date: Aug 2004
Posts: 2
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
Reply With Quote
  #2 (permalink)  
Old 08-11-04, 13:43
Pat Phelan Pat Phelan is offline
Resident Curmudgeon
 
Join Date: Feb 2004
Location: In front of the computer
Posts: 12,609
I'd use the TIMESTAMPDIFF function.

-PatP
Reply With Quote
  #3 (permalink)  
Old 08-11-04, 18:56
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
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
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #4 (permalink)  
Old 08-12-04, 05:31
Begar Begar is offline
Registered User
 
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
Reply With Quote
  #5 (permalink)  
Old 08-12-04, 06:16
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
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

Quote:
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
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On