我有一个名为“ example”的表,在“ colname2”一列中有如下字符串数据。
colname1 colname2 colname3
101 this is - test
50 this is - test2
105 this is - test31ws
我需要做以下事情
所以输出应该像这样
colname1 colname2 colname3
101 this is - test test
50 this is - test2
105 this is - test31ws test31ws
我正在使用oracle数据库。
答案 0 :(得分:1)
您可以使用case
和regexp_substr()
;
select t.*,
(case when colname1 > 100 then trim(regexp_substr(colname2, '[^-]+', 1, 2))
end) as colname3
from t;
如果没有连字符,它将返回NULL
。
答案 1 :(得分:0)
SUBSTR
是最简单的,可能也是最有效的。这是一个示例:
SQL> create table example
2 (colname1 number,
3 colname2 varchar2(20),
4 colname3 varchar2(20));
Table created.
SQL> insert into example
2 (select 101, 'this is - test' , null from dual union
3 select 50 , 'this is - test2' , null from dual union
4 select 105, 'this is - test31ws', null from dual
5 );
3 rows created.
SQL> update example set
2 colname3 = trim(substr(colname2, instr(colname2, '-') + 1))
3 where colname1 > 100;
2 rows updated.
SQL> select * from example;
COLNAME1 COLNAME2 COLNAME3
---------- -------------------- --------------------
50 this is - test2
101 this is - test test
105 this is - test31ws test31ws
SQL>