2015-10-30 102 views
0

我有一個輸入列,其值低於以下值。轉換爲特殊字符的字符串中的數字

1.2% 
111.00$ 
$100.00 
aa 
ss 

總產值有望

1.2 
111.00 
100.00 
null 
null 

我試圖使用REGEXP_REPLACE並試圖替換每個字符不能是數字或「」因此1.2%將變爲1.2。 這是我試過的查詢,但是這沒有奏效。

regexp_replace('%1.aa2', '[^[\d|\.]]', '') 

任何人都可以建議如何做到這一點?我做錯了什麼?我正在使用pl/sql開發人員處理Oracle 11.2數據庫。

+1

只要使用'+'在模式中。 – hjpotter92

+0

這沒有奏效,它取代了除「。」之外的所有內容。這裏是我使用的查詢 從雙 – Nitesh

+0

select select regexp_replace('%1.aa2','[^ \ d。] +','')我想你在這種情況下也必須避開反斜線。試試'\\ d'而不是'\ d'。 – hjpotter92

回答

1

使用POSIX類匹配數字字符,即[:digit:]。所以這個否定類[^[:digit:].]應該匹配任何字符,而不是點或數字。

regexp_replace('%1.aa2', '[^[:digit:].]', '') 
+0

這與 '有什麼不同?[^ [\ d。]] ? – Nitesh

+0

不知道oracle是否支持'\ d'。如果支持,那麼它會是'[^ \ d。]' –

+0

謝謝@Avinash raj。 [:數字]工作正常。我相信oracle也支持'\ d'。檢查此聲明 從雙重選擇regexp_replace('asas12313','\ d',' - ') – Nitesh

1

您可以使用[^0-9.]正則表達式,並用空字符串替換:

with testdata(txt) as (
    select 'ss' from dual 
    union 
    select 'aa' from dual 
    union 
    select '$100.00'  from dual 
    union 
    select '111.00$'    from dual 
    union 
    select '1.2%'    from dual 
) 
select regexp_replace(txt, '[^0-9.]', '') 
from testdata; 

SQL fiddle的結果:[^ \ d]

enter image description here

相關問題