小数点在更新字符变量之前被删除

时间:2018-12-31 12:47:01

标签: sql oracle

我具有以下格式的数据,并希望使用以下值更新类型varchar2的目标表列。但是问题在于,通过使用前导.462来修剪,它更新为0.462而不是'0'

source              destination column
-----------------   ------------------
0000004.304300000   4.3043
0000005.504500000   5.5045
0000141.400000000   141.4
0000138.900000000   138.9
0000000.462000000   0.462
0000000.000297000   0.000297

1 个答案:

答案 0 :(得分:1)

使用适当的格式掩码进行一点TO_CHARTO_NUMBER处理可能会完成这项工作。看一个例子:

SQL> create table test (source varchar2 (20), destination varchar2(20));

Table created.

SQL> insert into test (source)
  2    select '0000004.304300000' from dual union all
  3    select '0000000.462000000' from dual union all
  4    select '0000141.400000000' from dual union all
  5    select '0000033.000000000' from dual;

4 rows created.

SQL> alter session set nls_numeric_characters = '.,';

Session altered.

SQL> update test set
  2    destination = rtrim(to_char(to_number(source), 'fm999990D99999999'), '.');

4 rows updated.

SQL> select * From test;

SOURCE               DESTINATION
-------------------- --------------------
0000004.304300000    4.3043
0000000.462000000    0.462
0000141.400000000    141.4
0000033.000000000    33

SQL>