今天在轉換數據時,遇到了一個主鍵排序的問題。字符型的主鍵,保存的都是數字,數據導過來以后發現數據排序都是亂的,就想着按數字規則排序。
但發現to_number總是報錯,就想着里面應該是有字符存在。后來使用了正則關系式,問題解決。
以下是正則關系式的兩種用法,記錄下來。
方法一:
select * from xtyhxx order by to_number(translate(yhid, '0123456789.' || yhid, '0123456789.')) asc nulls last
方法二:
select * from xtyhxx order by to_number(regexp_substr(yhid,'[0-9]*[0-9]',1))
方法一更為准確。