1

I have table with field called description which contains text like following

|Description                              |
|-----------------------------------------|
|This is a text <a>blah</a> <br/> <img /> |
|This is <b>second</b> <a>row</a>         |

I would like to remove everything between "<" and ">"

Required Output

|Description                              |
|-----------------------------------------|
|This is a text blah                      |
|This is second row                       |

Is there anyway I can achieve this using MYSQL query. I cant currently use Stored procedure for the same as I don't have rights. I have requested for the access to use stored procedure but that will take some time and I have to finish this bit urgently.

Thanks.

Update: my question is similar to Remove string between 2 characters from text string but I want it to be done in MYSQL query.

5
  • You could probably do it with regex. Commented Sep 11, 2012 at 16:18
  • Both of you, regex won't do it, since it only does matching, not replacement. Commented Sep 11, 2012 at 16:19
  • @slugonamission I dont want to update my table. I just want output to be modified. Commented Sep 11, 2012 at 16:24
  • @KuldeepDaftary - it's still impossible, I was meaning that it can't do replacement even at the select stage. Commented Sep 11, 2012 at 16:29
  • Alright after long research I didnt really find a way to solve it using query hence used Stored Procedure. Commented Sep 17, 2012 at 10:12

4 Answers 4

3

In short, you can't, you need a more advanced language like PHP. Your SQL query is not the place to be doing string manipulation.

Sign up to request clarification or add additional context in comments.

1 Comment

You could do it using concatenation and substrings, but that would require you to know how many tags each field contained.
1

How about REPLACE() using a wildcard to capture the unknown text between opening and closing brackets? http://dev.mysql.com/doc/refman/5.0/en/string-functions.html#function_replace

Comments

0

if you use php as your server side language you can use strip_tags function to remove tags.I hope it can help you.

Comments

0

since this question is still in top google results, maybe the answer might help someone:

a quick function that replaces the content between two characters. It should work with words as well, but I haven't test it with words. For deleting in the following string: "this is a [demo]"

the string between [ and ] do:

select deletestr("this is a [demo]" ,'[',']');

and the function:

 CREATE DEFINER=`replace_with_your_db`@`%` 
FUNCTION `deletestr` (`inputstr` TEXT CHARSET utf8, `fromstr` VARCHAR(10) CHARSET utf8, `tostr` VARCHAR(10) CHARSET utf8) RETURNS text CHARSET utf8
    NO SQL
RETURN
replace(inputstr,substr(inputstr, locate(fromstr,inputstr),locate(tostr,inputstr)-locate(fromstr,inputstr)+length(tostr)),'')

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.