Oracle PL/SQL查询语句有的时候要将number类型的字段转换成varchar2类型 在报表或页面上经常会出现:
.440
.441
1.0
10.100
之类的数据,要不就是小数点前面的0被to_char或cast函数去掉了,或是末尾的无效小数位上的0没有被去掉,很是闹心。
jsp界面上还好处理,可以用类似下面的方法来处理:
Jsp代码
- 体重:<ww:text name="format.num.3"><ww:param value="person.weight"/></ww:text>(Km)
体重:<ww:text name="format.num.3"><ww:param value="person.weight"/></ww:text>(Km)
但是在超级报表中,单元格中要显示的数字就不太好处理了,要将查询出的字段进行处理,
下面我总结了一下处理方法:
step1:格式化将小数点前的0保留、去掉末尾的0,去掉前面因格式化产生的多余的空格: Plsql代码
- select t.num from(
- select ltrim(rtrim(to_char(0.44, "99990.000"), "0") , " ") as num from dual union
- select ltrim(rtrim(to_char(0.441, "99990.000"), "0"), " ") as num from dual union
- select ltrim(rtrim(to_char(1.0, "99990.0"), "0"), " ") as num from dual union
- select ltrim(rtrim(to_char(10.100, "99990.000"), "0"), " ") as num from dual
- )t
-
- /*
- 结果:
- 0.44
- 0.441
- 1.
- 10.1
- */
select t.num from(select ltrim(rtrim(to_char(0.44, "99990.000"), "0") , " ") as num from dual unionselect ltrim(rtrim(to_char(0.441, "99990.000"), "0"), " ") as num from dual unionselect ltrim(rtrim(to_char(1.0, "99990.0"), "0"), " ") as num from dual unionselect ltrim(rtrim(to_char(10.100, "99990.000"), "0"), " ") as num from dual)t/*结果:0.440.4411.10.1*/
step2:去掉可以多余的小数点: Plsql代码
- select
- (case when instr(t.num,".")=length(t.num) then rtrim(t.num,".") else t.num end) as num
- from(
- select ltrim(rtrim(to_char(0.44, "99990.000"), "0") , " ") as num from dual union
- select ltrim(rtrim(to_char(0.441, "99990.000"), "0"), " ") as num from dual union
- select ltrim(rtrim(to_char(1.0, "99990.0"), "0"), " ") as num from dual union
- select ltrim(rtrim(to_char(10.100, "99990.000"), "0"), " ") as num from dual
- )t
-
- /*
- 结果:
- 0.44
- 0.441
- 1
- 10.1
- */
SQL语句执行顺序Oracle数据库字典表优化小技巧相关资讯 Oracle基础教程
- Oracle块编程返回结果集详解 (11/10/2013 10:45:58)
- Oracle基础教程之设置系统全局区 (08/22/2013 14:24:00)
- Oracle基础教程知识点总结 (06/18/2013 07:43:32)
| - Oracle基础教程之tkprof程序详解 (10/22/2013 11:49:50)
- Oracle基础教程之sqlplus汉字乱码 (07/18/2013 16:30:00)
- Oracle 管理之 Linux 网络基础 (02/16/2013 18:37:35)
|
本文评论 查看全部评论 (0)