Re: INTERVAL Calculations
Posted in 1995
Thomas, Congratulations, and thanks! I hadn't realised that was possible, and it is very useful. Unfortunately, it only works in version 6.0x I4GL (or I4GL-RDS). As I'm sure that you are aware, you have to go through the character variable. Your code, in fact, does not use the intermediary variable dur_char. If you ran the posted code with WHENEVER ANY ERROR STOP you will find that the calculation fails at the division. With default error handling, you get a NULL result. See my code below... In ESQL/C, the invextend() function is documented in the 6.0 and later ESQL/C manuals, and demonstrates this functionality. It is not documented in the earlier (5.0, 4.x) manuals, and although it seems like it should exist, it does not. Consequently, the code below fails when run under 4.1x I4GL, and the function is not present in the 5.0x ESQL/C library. The 6.0x I4GL code uses calculation routines also used in the Engines, and the 6.0x Engines clearly handle these conversions -- the 4.1x and 5.0x Engines presumably do not. The prototype function declaration is: int invextend(intrvl_t *iv, intrvl_t *ov) The interval in iv is converted to the value in ov; the qualifier of ov should be set correctly (to the desired interval type) before the call. The return value is 0 for conversion OK, negative if the conversion failed and the value is the error number. Yours, Jonathan Leffler (johnl@informix.com) #include <disclaimer.h> MAIN DEFINE i1 INTERVAL DAY TO MINUTE DEFINE i2 INTERVAL MINUTE(9) TO MINUTE --DEFINE i3 INTEGER DEFINE c3 CHAR(10) DEFINE i4 INTEGER DEFINE m1 MONEY(16,4) DEFINE r1 DECIMAL(20,6) --DEFINE r2 DECIMAL(20,6) WHENEVER ANY ERROR STOP LET m1 = 123456.789 LET i1 = INTERVAL(1 12:30) DAY TO MINUTE LET i2 = i1 --LET i3 = i2 -- Conversion fails -1260 LET c3 = i2 LET i4 = c3 DISPLAY "m1 = ", m1 DISPLAY "i1 = ", i1 DISPLAY "i2 = ", i2 --DISPLAY "i3 = ", i3 DISPLAY "c3 = ", c3 DISPLAY "i4 = ", i4 --LET r1 = m1 / i2 -- Conversion fails -1260 --DISPLAY "r1 = ", r1 --LET r2 = m1 / i3 -- i3 is null --DISPLAY "r2 = ", r2 LET r1 = m1 / i4 DISPLAY "r1 = ", r1 END MAIN >From: WWHF15A@prodigy.com (Thomas Barnes) >Date: 31 Aug 1995 01:33:20 GMT >X-Informix-List-Id: <news.16679> > >Gregory Lynn Wood <lynnwood@crash.cts.com> wrote: >>I have a table that contains two columns. >>One is dollars, type is money. >>Second is duration, type is interval hour to minute. >> >>I wish to divide the dollars by the duration in order to >>get price per minute. >> >>How do I convert interval to an integer that I can do >>calculations with. > >If you are using 4GL all you need to do is define a second variable as in >interval minute(n) to minute where n is the maximum nuber of digits >needed. >Then define a char variable large enough to hold that value. Then divide. > >Example: > > define duration interval hour to minute, > amount money, > dur_min interval minute(9) to minute, > dur_chr char(10), > cost money(7,4) > > let dur_min = duration > let dur_char = dur_min > > let cost = amount/dur_min > >4GL will allow you to divide a number by a char as long as it makes sense >to do so (i.e. the char represents a real number) > >Hope that helps. I don't know if this will work in ESQL or not. > >THOMAS BARNES - WWHF15A@prodigy.com