Need to replace only first occurrence of string I hope I worded the question right. Here are the specifics:

This is regarding a MySQL database. I've inherited several hundred posts with custom fields and a unique excerpt. At this point I think I can solve the problem by inserting the &lt;!--More--&gt; tag into each post at a specific point. I need to do a search and replace with the following parameters:


Search each post_content field for the first occurrence of &lt;/em&gt;
Insert the &lt;!--More--&gt; tag right after the &lt;/em&gt;


Unfortunately each post has multiple &lt;em&gt; tag pairs, and if a post has more than one &lt;!--More--&gt; tag in it, WordPress just ignores all of them. So I need to so some sort of search and replace, but I have had no success after two days of trying every possible code snippet, SQL query and plugin I could find.
This may come across as silly, but if the CMS ignores subsequent &lt;!--more--&gt; tags, you shouldn't really care if it adds one after every &lt;/em&gt; closing tag. Sure it may make the posts slightly larger than they should be, but since they're just going to be ignored, it seems silly to spend two days trying to not do that when at the end of the day it doesn't really matter whether you do or not.

That said, I am not a MySQL guy, but looks like this is as decent an approach as any, from this StackOverflow answer:

UPDATE wp_posts
 SET post_content = CONCAT(REPLACE(LEFT(post_content, 
   INSTR(post_content, '&lt;/em&gt;')+4), '&lt;/em&gt;', '&lt;/em&gt;&lt;!--more--&gt;'), 
   SUBSTRING(post_content, INSTR(post_content, '&lt;/em&gt;') + 5))
 WHERE INSTR(post_content, '&lt;/em&gt;') &gt; 0;


or the (slightly simpler):

UPDATE wp_posts
 SET post_content = CONCAT(LEFT(post_content, INSTR(post_content, '&lt;/em&gt;')-1),
                           '&lt;/em&gt;&lt;!--more--&gt;',
                           SUBSTRING(post_content, INSTR(post_content, '&lt;/em&gt;')+ 5))
 WHERE INSTR(post_content, '&lt;/em&gt;') &gt; 0;


Before you do that you may want to check to see how many rows this will affect:

SELECT COUNT(*)
  FROM wp_posts
  WHERE INSTR(post_content, '&lt;/em&gt;') &gt; 0;


You'll probably want to add a WHERE clause to only identify those posts that actually contain an &lt;/em&gt; tag (and you may need to define the requirements for what to do in those cases).