5月12

SQL SELECT 小数0丢失(存储过程中为什么会丢失小数点前面的0)

| |
21:44 DBA  From: 本站原创
SQL SELECT 小数0丢失(存储过程中为什么会丢失小数点前面的0)
例如:数据库中值为0.85,select查询到页面显示为.85,小数点前面的0丢失!
我是这么处理的
select prize from database with(nolock) where pid=1
rs("prize ")
dim prize :prize =round(rs("prize "),2)
下面是网路上的一些解决方法
oracle数据库中,小数如果小于1,那么查询出来后前面的0是不显示的。所以不过是sql直接取出还是存储过程中取出,显示到页面时,都缺少小数点前的0.
解决办法
通过decode函数
如select decode(substr(percent,1,1),'.','0'||percent,percent) percent
from point;


关于sql丢失前缀零解决方案
SELECT round(-0.23,2)||'%' FROM dual; -.23%
解决
select to_char(round(-0.23,2),'90D99') from dual
SELECT to_char(-0.23, 90.99)||'%' FROM dual;
-0.23%

附赠round使用
ROUND函数是四舍五入函数,返回数字表达式并四舍五入为指定的长度或精度。

语法:
ROUND ( numeric_expression_r , length [ , function ] )
参数:
numeric_expression_r
精确数字或近似数字数据类型类别的表达式(bit 数据类型除外)。
length
是 numeric_expression_r 将要四舍五入的精度。length 必须是 tinyint、smallint 或int。当 length 为正数时,numeric_expression_r 四舍五入为 length 所指定的小数位数。当 length 为负数时,numeric_expression_r 则按 length 所指定的在小数点的左边四舍五入。
function
是要执行的操作类型。function 必须是 tinyint、smallint 或 int。如果省略 function 或 function 的值为 0(默认),numeric_expression_r 将四舍五入。当指定 0 以外的值时,将截断 numeric_expression_r。
返回类型
返回与 numeric_expression_r 相同的类型。
注释
ROUND 始终返回一个值。如果 length 是负数且大于小数点前的数字个数,ROUND 将返回 0。
示例
Result
Remark

select round(748.58,-1)
750.00
从小数点左边第1位四舍五入,其他位为0

select round(748.58,-2)
700.00
从小数点左边第2位四舍五入,其他位为0

select round(748.58,-3)
1000.00
从小数点左边第3位四舍五入,其他位为0

select round(748.58,-4)
0
length 是负数且大于小数点前的数字个数,ROUND 将返回 0

select round(748.58,1)
748.60
四舍五入为length所指定的小数位数

select round(748.58,2)
748.58
四舍五入为length所指定的小数位数

select round(124.9994,3)
124.9990
四舍五入为length所指定的小数位数

select round(124.9995,3)
125.0000
四舍五入为length所指定的小数位数

select round(150.75,0)
151.00
四舍五入的结果

select round(150.75,0,1)
150.00
截断的结果

最后编辑: tommyhu 编辑于2011/05/12 21:47
阅读(2880) | 评论(0) | 引用(0)
在此留下酱油瓶-:)
表情
emotemotemotemotemotemotemotemotemotemotemotemotemotemotemotemot
打开HTML
打开UBB
打开表情
隐藏
记住我

您的大名(例如:小明) : 

密码(可不填) :  游客无需密码

网址 (可不填) : 

电邮 (可不填) :  [注册]