网上有些对to_char函数的使用例子,大多如下
to_char(1210.73, '9999.9') would return '1210.7'
to_char(1210.73, '9,999.99') would return '1,210.73'
to_char(1210.73, '$9,999.00') would return '$1,210.73'
to_char(21, '000099') would return '000021'
其实,这样的例子不全,有些特殊情况会让你抓狂
order_amount NUMBER(22,2) //订单金额 2位小数
执行以下SQL
select t.order_amount ,
to_char(t.order_amount) a2,
to_char(t.order_amount, 'FM9999999999999999999.99') a3 ,
to_char(t.order_amount, 'FM9999999999999999990.00') a4 ,
to_char(t.order_amount, 'FM990.00')a5
from track_info t
结果
可见:
1.to_char的格式9或0的长度必须要大于实际数字的最大长度否则,出现#,如a5/6
1.如果是浮点数,特殊的是整数的个位数和小数部分的十分位,如果该格式对应位置为9,当整数部分只有个位且个位是0和小数部分是0时,会出现点号丢失0的情况
to_char(1210.73, '9999.9') would return '1210.7'
to_char(1210.73, '9,999.99') would return '1,210.73'
to_char(1210.73, '$9,999.00') would return '$1,210.73'
to_char(21, '000099') would return '000021'
其实,这样的例子不全,有些特殊情况会让你抓狂
order_amount NUMBER(22,2) //订单金额 2位小数
执行以下SQL
select t.order_amount ,
to_char(t.order_amount) a2,
to_char(t.order_amount, 'FM9999999999999999999.99') a3 ,
to_char(t.order_amount, 'FM9999999999999999990.00') a4 ,
to_char(t.order_amount, 'FM990.00')a5
from track_info t
结果
0 a1 a2 a3 a4 a5
1 0.30 .3 .3 0.30 0.30
2 1.30 1.3 1.3 1.30 1.30
3 1.31 1.31 1.31 1.31 1.31
4 100.00 100 100. 100.00 100.00
5 100.30 100.3 100.3 100.30 100.30
6 1000.00 1000 1000. 1000.00 #######
可见:
1.to_char的格式9或0的长度必须要大于实际数字的最大长度否则,出现#,如a5/6
1.如果是浮点数,特殊的是整数的个位数和小数部分的十分位,如果该格式对应位置为9,当整数部分只有个位且个位是0和小数部分是0时,会出现点号丢失0的情况