oracle PL SQL - 将数值转换为 varchar(百分比)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6923610/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 00:12:35  来源:igfitidea点击:

PL SQL - convert a numerical value into varchar (percent)

oracleplsql

提问by veg123

I have a query that uses the outputs(R1,R2) of two sub queries in order to divide them:

我有一个查询,它使用两个子查询的输出(R1,R2)来划分它们:

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.

除法器的输出是一个(十进制)数以及 R1、R2 的输出。

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

我想在结果中添加“%”符号(即 10,75%),但使用如下解决方案,返回错误 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

回答by zep

Welcome!

欢迎!

Replace the addition operator '+'with the SQL-Pl/sql concatenation operator: '||'.

将加法运算符'+'替换为 SQL-Pl/sql 连接运算符:' || '。

We can'tuse character literals directlyin number format modelslike in Datetime conversion.

我们不能像日期时间转换那样直接数字格式模型中使用字符文字。

SQL Concatenation operator

SQL 连接运算符

PL/SQL Concatenation operator

PL/SQL 连接运算符

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:

所有的“神谕”都在这里: