How to remove certain strings from rows in MySQL -
i attempting clean in messy table consisting of words unnecessary. example below shows typical content:
row1 | ------------- text <12> | more [dada] | (123) foo | la {55w} da | basically define unnecessary content words starting , ending particular symbol: <...>, [...], {...} , (...). use replace function, since data inside of symbols arbitrary not quite possible.
is possible use kind of regex in replace function?
update
please take notice content wrapped inside symbols can letters , numbers, unpredictable.
ok see !
use replace this - see example(will clean inside '()')
mysql> create table tbl ( -> txt varchar(255) -> ); query ok, 0 rows affected (0.50 sec) mysql> insert tbl values -> ('sometext (asdebtrw)'), -> ('some other text ( sd sdasddebtrw)'), -> ('somesdaftext ( (sd)( ))ebt rw)()'), -> ('sometext1'), -> ('sometext2'), -> ('sometext1 (replacethistext) anothertext1'), -> ('s'), -> ('w(sdf) rr') -> ; query ok, 8 rows affected (0.00 sec) records: 8 duplicates: 0 warnings: 0 mysql> select * tbl; +------------------------------------------+ | txt | +------------------------------------------+ | sometext (asdebtrw) | | other text ( sd sdasddebtrw) | | somesdaftext ( (sd)( ))ebt rw)() | | sometext1 | | sometext2 | | sometext1 (replacethistext) anothertext1 | | s | | w(sdf) rr | +------------------------------------------+ 8 rows in set (0.00 sec) mysql> update tbl -> set txt = replace(txt, substring(txt, locate('(', txt), length(txt) - locate(')', reverse(txt)) - locate('(', txt) + 2), '') -> txt '%(%)%'; query ok, 5 rows affected (0.20 sec) rows matched: 5 changed: 5 warnings: 0 mysql> select * tbl; +-------------------------+ | txt | +-------------------------+ | sometext | | other text | | somesdaftext | | sometext1 | | sometext2 | | sometext1 anothertext1 | | s | | w rr | +-------------------------+ 8 rows in set (0.22 sec)
Comments
Post a Comment