Subject Re: [firebird-support] Firebird ROUND
Author Helen Borrie
At 12:56 PM 15/01/2005 -0800, you wrote:

>Hi to everyone, I have a question... I need to round a decimal number as I
>used to do in SQL Server for example:
>
>Select Round(6.3254, 2) as Value_Rounded From MyTable
>
>The result looks like this:
>-------
>6.3300
>
>
>How can I do this in FB, I was using the FB_UDF but the ROUND Function
>only acept 1 parameter and I can't pass the decimals number that I wanna.

You can just use a double cast.

E.g.
select cast (cast (6.3254 as numeric (18,2)) as numeric(18,4)) as
Value_Rounded
from MyTable

This query will of course return a set of n identical values (where n is
the size of the set). I guess what you want in real life is

select cast (cast (AColumn as numeric (18,2)) as numeric(18,4)) as
Value_Rounded
from MyTable
where .....

But casting won't be useful if you have to pass the scale as an
argument. You would need a UDF for that, or you might write a
"pseudo-function" in PSQL using existing UDFs, that could be used in a
parameterised subquery, e.g.

create procedure roundme (
numval numeric (18,4),
return_scale smallint)
returns (outval numeric (18,4))
as
begin
outval = cast ((numval * dpower(10, return_scale)) as numeric(18,4));
outval = round(outval);
outval = cast ((outval / dpower(10, return_scale)) as numeric(18,4));
suspend;
end

So, this becomes a possibility:

select (
(select outval from roundme(mt.AColumn, ? ))) as Value_Rounded
from MyTable mt
where .....

./heLen