Subject RE: [firebird-support] Updating SQL with Trunc
Author Svein Erling Tysvær
>FB 1.5; no additional function added form a DLL
>
>I would like to run the following SQL but TRUNC is not a function. Is there a way to get around this (NAllow is a float)?
>
> Update MyData set
> NewAllow = Trunc(NAllow),
> New2Allow = Trunc((NAllow - Trunc(NAllow)) * 10);
>
>What I have is data like this:
>
>NAllow NewAllow New2Allow
>10.1 10 1
>5.3 5 3
>7.0 7 0
>0.4 0 4
>
>Can something like this be done in a SQL command?
>
>Thank you,
>
>Ed Dressel

Update MyData set
NewAllow = cast(NAllow - 0.5 as Integer),
New2Allow = cast(((NAllow - cast(NAllow - 0.5 as Integer)) * 10) - 0.5 as Integer);

should be close (remove the last - 0.5 from New2Allow if you want the decimal part to be rounded rather than truncated).

However, a float is not an exact number, and you have some problems with things never being stored as an exact number, e.g. when I tested I got

NAllow NewAllow New2Allow
7.000 7 -1

This was, of course, due to NAllow being stored as something like 6,9999999999463, and

(6,9999999999463 - 7) = -0,0000000000537

which, when multiplied by 10 and deducted 0.5 is

-0,500000000537

which of course is closer to -1 than 0. You can of course change - 0.5 to -0.499, but then you could get trouble with numbers like

NAllow NewAllow New2Allow
6.9995 7 -1

HTH,
Set