Subject Re: [firebird-support] How to cast a float?
Author Helen Borrie
At 10:14 PM 12/09/2004 +0000, you wrote:
>Hi,
>I have a float in a table which represents a percentage, eg 30.75
>
>When I run a particular query I want to return this value with a %
>symbol after it, i.e as 30.75%.
>If my query is:
>
> select cast(depreciation_rate as integer) || '%' from my_table
>
>This works fine, except of course only the integer part of the value
>is retained. So I changed the query to:
>
> select cast(depreciation_rate as char) || '%' from my_table
>
>But this causes an "Arithmetic overflow or division by zero... or
>string truncation".
>
>So what is the correct way to do this? (Alternatively, what is the
>correct way to return this float as a percentage value?)

Casting anythinng AS CHAR you are asking for a single byte string.
i.e. AS CHAR and AS CHAR(1) mean the same thing.

If you don't want a gazillion figures after, then double cast it, first to
numeric(5,3) and then to a varchar of suitable size.

select cast(cast(depreciation_rate as numeric(5,3)) as varchar(7)) || '%'
from my_table as DeprRate

./heLen