天天看點

oracle中判斷某列是否為數字-replace和translate函數詳解

簡要比較:
replace:字元串級别的代替
如: SELECT REPLACE (' acdd', 'cd','ef') FROM dual;     --aefd
translate:
字元級别的代替
如: SELECT TRANSLATE('acdd', 'cd', 'ef') FROM dual;  --aeff

解釋: repalce 中,每個search_ string 都被replacement string 所代替。
select replace ('acdd', 'cd', 'ef') from dual;   →aefd
如果replacement string 為空或為NULL,那麼所有的search string 都被移除。
select replace ('acdd', 'cd','') from dual;  -ad
如果search string 為null 那麼就傳回原來的char。
select replace ('acdd','', 'ef') from dual; +acdd
select replace('acdd','','') from dual;-acdd (也是兩者都為空的情況)
           

可以看出translate是逐字元替換的,

oracle中判斷某列是否為數字
1.使用trim+translate函數:
 select Case When trim(translate('2586.820258','0123456789.',' '))  is NULL 
 then 1 else 0 end as isnumeric from dual  --1 (純數字傳回1,否則傳回0)

select * from table where trim(translate(column,'0123456789',' ')) is NULL;
select * from k_dic st where regexp_like(st.id,'^[0-9]+[0-9]$');
sql server
select ISNUMERIC(isnull(100,0))

ORACLE取出字段裡的全部數字
SELECT(REGEXP_REPLACE('LSS12345', '[^0-9]')) FROM DUAL;---取出值裡面的全部數字