Remove everything after string

2019-06-27 23:09发布

I have a table with a field that contains descriptions for items sold on my site.

In our infinite wisdom, when we first started the site, we pasted code for a Facebook Like button into the description, each just a little different, as it has product info in the link. We now have about 400 items with the code in it.

This is an example of what I'm dealing with:

-Whipped Up, Fluffy Chocolate-On-Chocolate Taste; A Lighter Way To Enjoy Chocolate
- 45% Less Fat


< iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fwww.oursite.com%2Findex.php%3Fmain_page%3Dproduct_info%26cPath%3D33_36%26products_id%3D106&layout=standard&show_faces=true&width=450&action=like&colorscheme=light&height=80" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:450px; height:80px;" allowTransparency="true">< /iframe>

I want to remove the <iframe and everything past it. I have been looking around for examples using REPLACE and/or LEFT, but can't find what I need, as I am not very well versed in SQL language.

It will always be at the end of the products_description field, so I don't need to worry about saving anything past it, only before it.

The best I can come up with is

SELECT LEFT(REPLACE('<iframe%','<iframe%',' '),0)

but that doesn't seem to work.

Thank you for any help you can offer on this, as it will take me much less time than editing each description.

UPDATE: Still haven't found an answer, after trying many variations of what has been suggested. It runs, but makes no edit to the column.

I also tried this, after finding it on another site. The same, but thought it might give someone an idea of how to proceed:

select left(`products_description`,instr('<iframe',`products_description`)-1) FROM products_description

标签: sql replace
3条回答
时光不老,我们不散
2楼-- · 2019-06-27 23:38
SELECT 
trim( -- trim removes spaces before and after given string
    left(
        'some text < iframe', 
        locate('< iframe', 'some text < iframe') - 1
    )
);

For better understanding i did not removed space between < and iframe.

You may also take a look at this thread:

Remove HTML tags from record

but this is discussion about removing tags only, and keeping <tag>text</tag> between tags. Anyway it will for for you, because you have nothing between <iframe> and </iframe>.

查看更多
萌系小妹纸
3楼-- · 2019-06-27 23:43
  1. REPLACE('<iframe%','<iframe%',' ') will give you just ' ' - it searches first argument for second argument and replaces it with third.
  2. LEFT(somestring, 0) will give you 0 characters string
查看更多
霸刀☆藐视天下
4楼-- · 2019-06-27 23:45

Try this:

select substr(your_raw_string, 0, instr(your_raw_string, '<iframe') -1)
from your_table
查看更多
登录 后发表回答