MySQL Forums
Forum List  »  Newbie

Re: Type-Question
Posted by: Felix Geerinckx
Date: June 17, 2005 12:21PM

Dermy sqlheld wrote:

> The problem is, i want to calculate the average speed of the entries distance and time took,
> assumingly an easy thing... But the time is entered as above and i must recalculate it to an
> per-100-base-value, that is
> 1.25 --> 0.25 * 3/5 = 0.15 --> real time: 1.15 --> average speed = distance / real time

Are you sure about this formula? In my book, converting HH.MM to decimal format is done as follows: HH + MM/60


> If i have a column of type dec or float, for the entry time 1.25 i.e. (that means 1 hour and 25
> minutes), how can i get the fractional part of that number and how can i convert a float to a
> string?

If you run MySQL 4.1.1 or higher, none of that is necessary:

USE test;
DROP TABLE IF EXISTS foo;
CREATE TABLE foo (
d DECIMAL(5,2),
f FLOAT
);

INSERT INTO foo (d, f) VALUES (1.25, 1.25), (1.30, 1.30);

SELECT
TIME_TO_SEC(STR_TO_DATE(FORMAT(d,2), "%H.%i"))/3600 AS dn,
TIME_TO_SEC(STR_TO_DATE(FORMAT(f,2), "%H.%i"))/3600 as fn
FROM foo;

Otherwise, this will work also:

SELECT
FLOOR(d) + (d - FLOOR(d))*5/3 AS dn,
FLOOR(f) + (f - FLOOR(f))*5/3 AS fn
FROM foo;

--
felix
Please use BBCode to format your messages in this forum.

Options: ReplyQuote


Subject
Written By
Posted
June 14, 2005 05:31PM
June 15, 2005 12:52AM
June 15, 2005 08:40AM
June 16, 2005 02:11AM
June 17, 2005 08:21AM
Re: Type-Question
June 17, 2005 12:21PM
June 17, 2005 04:45PM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.