0

How can we achieve this in a single sql (mysql) query ?

I need to search some patterns in a column and if any pattern matches then replace that pattern with a specific string. like if

 pattern ptr1 matches then replace this with str1 pattern ptr2 matches then replace this with str2 

I need a function like replace that can replace a regular expression. here is my query i need to improve this for regular expression

UPDATE category SET column1 = ( CASE WHEN (column1 REGEXP 'xy') THEN REPLACE(column1, 'xy' , 'ffff') ELSE column1 END ) 

Please help me in this.

2
  • Thanks I tried that function but did not work. its working this way when i am passing a string like UPDATE category SET column1 = (CASE WHEN (column1 REGEXP '^xy') THEN regex_replace ('^xy' , 'ffff','kunal pawar') ELSE column1 END) but did not work when i pass column name like - UPDATE category SET column1 = (CASE WHEN (column1 REGEXP '^xy') THEN regex_replace ('^xy' , 'ffff',column1) ELSE column1 END) Commented Jun 3, 2013 at 8:04
  • If you solved the problem, go ahead and answer your own question. Commented Jun 3, 2013 at 8:10

1 Answer 1

0

you can put multiple replace function while updating table values -

UPDATE category SET column1 = REPLACE(REPLACE(column1, ptr1, str1), ptr2, str2) 
Sign up to request clarification or add additional context in comments.

1 Comment

actually my problem is like that - suppose i have a column name and then if i get kunal# in name then kunal# should be replaced by pawar and if i find kunal@ in the name then it should be replaced by pawar-1 for ex hidfds kunal# 434fdwef -> hidfds pawar 434fdwef hidfds kunal@ 434fdwef -> hidfds pawar-1 434fdwef

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.