你的 Oracle 版本是多少呀?
我这里测试了, 你那种写法,是没有问题的啊。
CREATE TABLE test_tochar(
id INT,
val decimal(10,4)
);
INSERT INTO test_tochar VALUES(1, 0.0001);
INSERT INTO test_tochar VALUES(2, 0.0012);
INSERT INTO test_tochar VALUES(3, 0.0123);
INSERT INTO test_tochar VALUES(4, 0.1234);
INSERT INTO test_tochar VALUES(5, 1.2345);
INSERT INTO test_tochar VALUES(6, 12.3456);
INSERT INTO test_tochar VALUES(7, 123.4567);
INSERT INTO test_tochar VALUES(8, 1234.5678);
INSERT INTO test_tochar VALUES(9, 12345.6789);
默认的显示方式。
SQL> COLUMN "Result" FORMAT A15
SQL> SELECT id, TO_CHAR(val) Result FROM test_tochar;
ID RESULT
---------- ---------------
1 .0001
2 .0012
3 .0123
4 .1234
5 1.2345
6 12.3456
7 123.4567
8 1234.5678
9 12345.6789
已选择9行。
注意到, 小于 1 的数据, 被 to_char 成 .0001 了。 而不是 0.0001
修改 TO_CHAR 的格式化参数:
SQL> SELECT id, TO_CHAR(val, '999,990.999') Result FROM test_tochar;
ID RESULT
---------- ---------------
1 0.000
2 0.001
3 0.012
4 0.123
5 1.235
6 12.346
7 123.457
8 1,234.568
9 12,345.679
已选择9行。