今天看到一个这样的sql :
select ccode,
times,
pt,
to_char(last_value(decode(pt, 0, null, pt) ignore nulls)
over(partition by ccode order by times),
'fm90.09') new_pt
from tmp_t;
对to_char(value,'fm90.09')里面的fm90.09很陌生,虽然可以猜出是格式化用的,但是里面的0,9代表什么不知道。
to_char用法:TO_CHAR ( n [, fmt [, 'nlsparam']] )
官方文档链接:http://docs.oracle.com/cd/B19306_01/server.102/b14200/sql_elements004.htm,里面有详细的介绍.
下面我要说的是to_char fm90.09代表什么,参考了博文http://www.cnblogs.com/liubiqu/archive/2008/01/17/1042403.html。
总结如下:
1,有9的地方如果有数字就显示如果没有数字就不显示,有0的地方在没有数字的时候也会有0来占位
select to_char(9999.09556,'fm99999.0900'),to_char(9999.09556,'fm00099.0900') from dual
结果为:
2,截取小数的时候是四舍五入
select to_char(90.99,'fm999.0')保留一位小数,to_char(90.99,'fm999.00')保留2位小数 from dual
结果为:
3,如果用fm9.99 整数仍然会显示.,如果不需要需要替换
select to_char(9,'fm99.99'),regexp_replace(to_char(9,'fm99.99'), '\.$', '') from dual
4,fm中整数部分需大于等于值的整数部分长度
select to_char(99999.09, 'fm9.09'),
to_char(99999.09, 'fm99.09'),
to_char(99999.09, 'fm999.09'),
to_char(99999.09, 'fm9999.09'),
to_char(99999.09, 'fm99999.00')
from dual
结果为:
select to_char(99999.09, 'fm0.09'),
to_char(99999.09, 'fm00.09'),
to_char(99999.09, 'fm000.09'),
to_char(99999.09, 'fm0000.09'),
to_char(99999.09, 'fm00000.00')
from dual
结果为:
综合例子为:
with tmp_t as(
select 1 as v_id,0.02 as v_value from dual union all
select 2,10.02 from dual union all
select 3,0.054 from dual union all
select 4,20.01 from dual union all
select 5,-21.006 from dual union all
select 6,-1 from dual union all
select 7,-2.004 from dual union all
select 8,20.046 from dual)
select v_id,
v_value,
to_char(v_value)v_1,
to_char(v_value, 'fm90.09')v_2,
regexp_replace(to_char(v_value, 'fm90.9'), '\.$', '') v_3,
regexp_replace(to_char(v_value, 'fm90.09'), '\.$', '') v_4,
regexp_replace(to_char(v_value, 'fm90.09'), '\.0*$', '') v_5
from tmp_t
结果为:
如有错误,欢迎提出,谢谢。
全文完。