2

This is one of those "can it be done" questions. I had a colleague approach me about removing web addresses - all unique except starting with http - from databased text strings. My first instinct was to go with the replace function but that can become quite cumbersome to use and maintain.

So I'm querying the forum on their thoughts on how to best approach this task.

 This is a test http://t.co/aBc689XYz -> new result=This is a test
 Have a nice http://t.co/vZ754PlkuI day -> new result=Have a nice day
9
  • Post some samples on existing data. And samples on expected results. Commented Apr 23, 2014 at 17:41
  • @Ravinder examples posted Commented Apr 23, 2014 at 17:46
  • Does this have to be done in MySQL only or is this better suited to be done at the application layer? Commented Apr 23, 2014 at 17:47
  • you'll have to skim this task in scripting language, MySQL or RDBM is not advisable for this job!! :) Commented Apr 23, 2014 at 17:48
  • @MikeBrant right now he is looking at mysql Commented Apr 23, 2014 at 17:48

1 Answer 1

3

If the URL part exists only once in the text, the following should be working.

MySQL Solution:

select concat( @pss:=substring_index( txt, 'http://', 1 ), 
               substring( @ss:=substring_index( txt, 'http://', -1 ), 
                          if( (@l:=locate( ' ', @ss )) > 0, @l+1, 0 ) ) 
       ) as txt
from (
  select 'This is a test http://t.co/aBc689XYz' as txt
  union all
  select 'Have a nice http://t.co/vZ754PlkuI day'
  union all
  select 'This worked http://sqlfiddle.com/#!2/d41d8 perfectly on sql fiddle'
) records
;

Results:

+-------------------------------------+
| txt                                 |
+-------------------------------------+
| This is a test                      |
| Have a nice day                     |
| This worked perfectly on sql fiddle |
+-------------------------------------+

Demo @ MySQL 5.5.32 Fiddle

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

5 Comments

question : assuming the txt column contains 100 rows....how do u plan to loop over it for pattern replacing?
@Ravinder I'll definitely give this a go!
@Ravinder The code worked perfectly! Only hiccup was some text whereby http was entered twice. Thanks again.
@Atwp67: Yes. And I mentioned the same, in the first line of my answer.
@Ravinder I know, but you got me 99.9% of the way there. Again, I appreciate your help.

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.