PL SQL - convert a numerical value into varchar (percent)
I 开发者_Go百科have a query that uses the outputs(R1,R2) of two sub queries in order to divide them:
select a.R1/b.R2*100.0 as Result
from
(query1) a,
(query2) b
The division's output is a (decimal) number as well as the R1,R2 outputs.
I want to add to the Result the '%' sign, (i.e 10,75 %), but using a solution like the below one, returns an error 01722=invalid number
select cast(cast(a.R1/b.R2*100.0 as decimal(10,2)) as varchar(10)) + '%' as Result
from
(query1) a,
(query2) b
Welcome!
Replace the addition operator '+' with the SQL-Pl/sql concatenation operator: '||'.
We can't use character literals directly in number format models like in Datetime conversion.
SQL Concatenation operator
PL/SQL Concatenation operator
Number format models
Examples:
select cast(round(10.75, 2) as varchar(10)) || '%' as result
from dual
select to_char(round(1234567890.29, 2),'9G999G999G999D99' ) || '%' as result
from dual
PS. The route is always the same:
How to ask
All the "oracles" are here:
精彩评论