2016-05-25 58 views
-1

我有一個oracle 11g表。列A中的數據是RS_X,RS_2,RS_3等。oracle 11g正則表達式

如果RS_之後的數據是'X',那麼輸出應該設置爲'-1'。

有人請讓我知道我該怎麼做。

感謝 帕

回答

0

您可以使用select解碼和SUBSTR

select decode(substr(columnA, 4,1),'X', -1, columnA) from my_table; 

或WHERE條件

select -1 from my_table 
where substr(columnA, 4,1) = 'X' ; 
+0

substr需要從4而不是3來完成,因爲3是'_'。 – SubhasisM

+0

@SubhasisM謝謝我更新答案 – scaisEdge

0

使用下面的查詢來獲取輸出 -

select col1,decode(substr(col1,4,1),'X',-1,1) from test1 

輸出會是這樣 -

RS_1 1 
RS_2 1 
RS_3 1 
RS_X -1 
1

如果你想這樣做,使用正則表達式,然後使用這個SQL。

update my_table 
set output = -1 
where regexp_like(columnA, '^RS_X') 

與「RS_X」他們在columnA值開頭的所有記錄將設置爲-1的輸出值。