2

I have a table with rows like this:

|column A                              |

|normal data 1 (some data to remove 1) |
|normal data 2 (some  data to remove 2)|
|normal data 3 (some  data to remove 3)|
|normal data 4 (some  data to remove 4)|
|........                              |

and I want to update my rows to keep only normal data and delete data inside "()" I'm wondering if there is a way to use regex inside my SQL update statement to delete all data after " ("

Dharman
  • 26,923
  • 21
  • 73
  • 125
James
  • 915
  • 4
  • 16
  • 40
  • http://stackoverflow.com/questions/986826/how-to-do-a-regular-expression-replace-in-mysql – Ben Mar 21 '17 at 20:50

3 Answers3

1

I think the easiest way is substring_index():

update t
    set col = substring_index(col, ' (', 1)
    where col like '% (%';
Gordon Linoff
  • 1,198,228
  • 53
  • 572
  • 709
1

You must use 2 SUBSTRING_INDEX like this if there also some data behind the ).

SELECT SUBSTRING_INDEX(SUBSTRING_INDEX( YourFieldName , '(', 1),')',-1);

sample

mysql> SELECT SUBSTRING_INDEX(SUBSTRING_INDEX('normal data 1 (some data to remove 1)', '(', 1),')',-1);
+------------------------------------------------------------------------------------------+
| SUBSTRING_INDEX(SUBSTRING_INDEX('normal data 1 (some data to remove 1)', '(', 1),')',-1) |
+------------------------------------------------------------------------------------------+
| normal data 1                                                                            |
+------------------------------------------------------------------------------------------+
1 row in set (0,00 sec)

mysql>
Bernd Buffen
  • 13,762
  • 2
  • 22
  • 34
1

Try to use replace, substring and position functions to eliminate the data between ().

update tablename set col=replace(col,substr(col,position('(' in col),position(')' in col)-position('(' in col)),' ');

then it will update all the data between () inclunding braces.

Rams
  • 2,109
  • 1
  • 11
  • 19