在更新中使用通配符?

时间:2010-04-23 13:09:45

标签: sql oracle

我想通过一个表,并将'notify4-N'的所有实例更改为'notify5-N',其中N是1-9中的数字。有没有办法在SQL中这样做?在perl中这很容易,但我不确定客户是否在服务器上安装了perl。

3 个答案:

答案 0 :(得分:6)

您可能正在寻找REGEXP_REPLACEREGEXP_LIKE功能以及更新。

 update sometable set somecol = REGEXP_REPLACE(somecol, ...) where REGEXP_LIKE(somecol, ...) 

答案 1 :(得分:2)

显示将在更新中使用的值。 where条件确保notify4-11保持不变。

create table notify(n varchar(20));

insert into notify(n) values('notify4-0');
insert into notify(n) values('notify4-1');
insert into notify(n) values('notify4-2');
insert into notify(n) values('notify4-8');
insert into notify(n) values('notify4-9');
insert into notify(n) values('notify4-11');
select n, regexp_replace(n,'^notify4-([1-9]{1})$', 'notify5-\1') from notify where regexp_like(n, '^notify4-[1-9]{1}$') order by n;

答案 2 :(得分:1)

未经测试,但是:

UPDATE my_table SET name = 'notify5-' || SUBSTR(name, 9) WHERE name LIKE 'notify4-%'

这适用于不支持正则表达式匹配的数据库服务器。 :-)(但是,我看到你的帖子被甲骨文标记,所以,我认为克劳斯的答案对你也有用。)