Casting interval to real
Posted in 1999
Topics: General Discussion
Hi there.. I need a function to cast a interval type to a real value. For example I would like to find out that a field of type interval hour to second which has a value of '0:30:00' is 0.5 hours. I need this to calculate average speed. I'm developing a database for rallying and I know the length of one leg in kilometers, and in another table I have the time that it takes each crew to drive that leg in interval hour to fraction(2) format. Does anyone see a quick solution for this other than writing a complicated SP? Arnar
Arnar Birgisson wrote:
>
> Hi there..
>
> I need a function to cast a interval type to a real value. For example I
> would like to find out that a field of type interval hour to second which
> has a value of '0:30:00' is 0.5 hours.
>
> I need this to calculate average speed. I'm developing a database for
> rallying and I know the length of one leg in kilometers, and in another
> table I have the time that it takes each crew to drive that leg in interval
> hour to fraction(2) format.
>
> Does anyone see a quick solution for this other than writing a complicated
> SP?
How about a simple SP that you could also implement, pretty much unchanged
except for syntax, in 4GL or ESQL/C:
CREATE PROCEDURE frac_hours ( input INTERVAL HOUR(2) TO FRACTION(2) )
RETURNING DECIMAL(16,7); DEFINE holder, output DECIMAL(16,7);
DEFINE all INTERVAL SECOND(9) TO FRACTION(2);
DEFINE str1 CHAR(50);
LET all = input;
LET str1 = all;
LET holder = str1;
LET output = holder / 3600.00;
return output;
END PROCEDURE
DOCUMENT
"Convert INTERVAL HOUR(2) TO FRACTION(2) to float as fractional hours"
;
Art S. Kagel
Art S. Kagel wrote:
> Arnar Birgisson wrote:
> > I need a function to cast a interval type to a real value. For
> > example I would like to find out that a field of type interval
> > hour to second which has a value of '0:30:00' is 0.5 hours.
> > [...snip...]
> > Does anyone see a quick solution for this other than writing a
> > complicated SP?
>
> How about a simple SP that you could also implement, pretty much
> unchanged except for syntax, in 4GL or ESQL/C:
>
> CREATE PROCEDURE frac_hours(input INTERVAL HOUR(2) TO FRACTION(2))
> RETURNING DECIMAL(16,7);> [...snip...]
And, if you're using ESQL/C, I posted a package of routines to
convert various interval types into decimals. This would probably
have been in Autumn 1998, maybe even earlier. With luck, it has
made its way into the IIUG archives -- but I've not verified that.
I'll repost if you assure me that DejaNews (www.deja.com)
does not have any record of it since Jan 1997.
--
Jonathan Leffler (jleffler@informix.com, jleffler@earthlink.net)
Guardian of DBD::Informix v0.60 -- see http://www.perl.com/CPAN
#include <disclaimer.h>