您的位置:首页 > 数据库 > Oracle

oracle数据中特殊字符处理

2014-10-23 10:06 423 查看
最近数据处理时经常发现有数字中含有大量字符,执行过程报“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


注:这些是以前所写,现转移到OSC,博客原文:http://www.cnblogs.com/godsweet/p/3274947.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: