Subject Re: CAST(var1 AS NUMERIC(18,:variable));
Author Adam
--- In firebird-support@yahoogroups.com, Luis Madaleno <luis@...> wrote:
>
> Hi,
>
> I need to use the CAST() function to emulate a ROUND, but the decimal
> places I want are not fix, they are based on a field value.
> So I thought I could do this:
>
> CREATE PROCEDURE MYPROC(VALUE NUMERIC(18,6)) RETURNS RESULT
NUMERIC(18,6);
> AS
> DECLARE VARIABLE DECIMAL_PLACES INTEGER;
> BEGIN
> SELECT DECIMAL_PLACES FROM MYTABLE INTO :DECIMAL_PLACES;
> RESULT = CAST(VALUE AS NUMERIC(18, DECIMAL_PLACES));
> END;
>
> This is not the real SP, is just to demonstrate my need.
> Is it possible to use the CAST in this way? Or can I achieve what I
need
> in any way?

I haven't checked, but I doubt you will be able to use parameters in
the cast function. You may be able to get what you want using execute
statement. Just don't forget to protect yourself against uninitialised
variables etc.

eg:

DECLARE VARIABLE STMT VARCHAR(100);
DECLARE VARIABLE DECIMAL_PLACES INTEGER;
BEGIN
DECIMAL_PLACES = 0;

SELECT FIRST 1 DECIMAL_PLACES FROM MYTABLE INTO :DECIMAL_PLACES;

IF (:DECIMAL_PLACES > 6) THEN
BEGIN
EXCEPTION ETooManyDP;
END

STMT = 'SELECT CAST(VALUE AS NUMERIC(18,' || :DECIMAL_PLACES ||'))
FROM RDB$DATABASE';
EXECUTE STATEMENT STMT INTO :RESULT;
END
^

Adam