Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using a wildcard in an update?

I want to go through a table, and change all instances of 'notify4-N' to 'notify5-N', where N is a number from 1-9. Is there a way to do that in SQL? It would be easy in perl, but I'm not sure the customer even has perl on his servers.

like image 497
Paul Tomblin Avatar asked Nov 23 '25 14:11

Paul Tomblin


2 Answers

You are probably looking for the REGEXP_REPLACE and REGEXP_LIKE function in conjunction with an update.

 update sometable set somecol = REGEXP_REPLACE(somecol, ...) where REGEXP_LIKE(somecol, ...) 
like image 65
Klaus Byskov Pedersen Avatar answered Nov 26 '25 06:11

Klaus Byskov Pedersen


This shows the values that would be used in the update. The where condition ensures that notify4-11 is left unchanged.

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;
like image 29
Janek Bogucki Avatar answered Nov 26 '25 04:11

Janek Bogucki



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!