关于去除数据中特殊字符(非数字字符)的一些方法

最近数据处理时经常发现有数字中含有大量字符,执行过程报“ORA-01722: invalid number”异常,可以有一下几种方法去除:
1、replace 用法简单,写法较复杂,只能处理已知字符

  With test_table1 As (
    Select 1 seq_num, '2134?654?ag d35' strings From dual
     Union All
    Select 2 seq_num, '651 354a g5 dd21' strings From dual
    ) 
Select seq_num,
 Replace(Replace(Replace(Replace(Replace(strings, '?', ''), 'a', ''), 'g', ''), 'd', ''), ' ', '') 
  From test_table1;

---执行结果
1    213465435
2    651354521

2、translate 用法简单,写法简单,只能处理已知字符,字符串、待查找字符,替换字符,均不能为null,否则返回null,字符串按查找顺序替换,若无则去除

  With test_table1 As (
    Select 1 seq_num, '2134?654?ag d35' strings From dual
     Union All
    Select 2 seq_num, '651 354a g5 dd21' strings From dual
    ) 
Select seq_num,
 translate(strings, '1asdfasg ?', '1') 
  From test_table1;

---执行结果
1    213465435
2    651354521

3、regexp_replace 正则表达式 增强型replace 参数多,可根据正则式处理所有字符

  With test_table1 As (
    Select 1 seq_num, '2134?654?ag d35' strings From dual
     Union All
    Select 2 seq_num, '651 354a g5 dd21' strings From dual
     Union All
    Select 3 seq_num, '2134654?ag d35' strings From dual
     Union All
    Select 4 seq_num, '16?54?aasdgf78as' strings From dual
     Union All
    Select 5 seq_num, '16?!@#$%^&*()~:"+_?><|~8as' strings From dual
    ) 
Select seq_num,
 regexp_replace(strings, '[^0-9]', '')
  From test_table1;

---执行结果
1    213465435
2    651354521
3    213465435
4    165478
5    168

 

posted @ 2013-08-22 15:18  mySweet  阅读(5251)  评论(4编辑  收藏  举报