2010-04-23 55 views
2

我想通過一個表格,並將'notify4-N'的所有實例更改爲'notify5-N',其中N是1-9的數字。有沒有辦法在SQL中做到這一點?在perl中很容易,但我不確定客戶甚至在他的服務器上安裝perl。在更新中使用通配符?

回答

6

您可能正在尋找REGEXP_REPLACEREGEXP_LIKE函數與更新結合使用。

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

沒有測試,但是:

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

這應該不支持正則表達式匹配的數據庫服務器。 :-)(但是,我看到您的帖子標有Oracle,所以我推測Klaus的答案也適用於您。)

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;