標籤:style blog http color io os ar sp 資料
最近資料處理時經常發現有數字中含有大量字元,執行過程報“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 Regex 增強型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, ‘[email protected]#$%^&*()~:"+_?><|~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
oracle資料中特殊字元處理