Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

subtract dates query help

Status
Not open for further replies.

DotNetGnat

Programmer
Mar 10, 2005
5,548
IN
Guys,

I am trying to subtract two dates(c.return, c.reserved as shown below) in an older mysql version in which new date functions doesnt work...

so far i have this query working fine...but i was looking for an efficient one...anyone has any suggestions on the efficiency improvement to this below query...

Code:
String selectStatement = "select count(*) as total_reservations, sum((((time_to_sec(c.return) - time_to_sec(c.reserved))/60/60) " 
      + " + ((to_days(c.return)-to_days(c.reserved))*9) "
      + " - ((week(c.return)-week(c.reserved))*18) "
      + " - ((year(c.return)-year(c.reserved))*936))) as total_hours";

thanks in advance...

-DNG
 
What sort of calculation is this? If you just wanted to calculate the hours difference between two date-time values, then the most efficient way would probably be to convert each date-time to seconds using UNIX_TIMESTAMP() and divide the difference by 3600.
 
Tony,

Does Unix_TimeStamp() work in older version of mysql...lower than mysql4.x version...

thanks

-DNG
 
if that function doesnt exist then what would you think the best and efficient alternative for what i am trying to do...thanks

-DNG
 
As far as I know, UNIX_TIMESTAMP was available from time immemorial. It's such a basic function that it's hard to imagine how MySQL could do without it. The alternative would be something along the lines of what you posted, which as you point out is not particularly efficient. Can you try UNIX_TIMESTAMP() and see what happens?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top