I need to extract text from a MySQL column and copy that text to another column. This text will be an excerpt of the full text in the first column in that I need to trim the 15 leading characters, and then include only the next 140 +/- characters of the text and copy that text to a new column.
This works, of course, to copy all text from one column to another:
UPDATE
tableSET column1=column2
And I see the functions LEFT and RIGHT to trim text from the left or the right.
But is it possible to combine LEFT and RIGHT to strip the first 15 characters and then copy the next + or - 140 characters?
There are no delimiters I can use; all the text will be different. So I need to be able to count characters.
And when I say + or - 140 characters: is it possible to not break a word that may end up at the end of the 140?
Update 11/27/13
This query below by Jungsu Heo works and doesn't break words for a table called wp2_posts
, and the columns post_excerpt
and post_content
:
UPDATE `wp2_posts`
SET post_excerpt = SUBSTRING(SUBSTRING(post_content, 55, 110), 1,
IF(LENGTH(SUBSTRING(post_content, 55, 110)) -
LENGTH(SUBSTRING_INDEX(SUBSTRING(post_content, 55, 110), ' ', -1)) = 0,
LENGTH(SUBSTRING(post_content, 55, 110)), LENGTH(SUBSTRING(post_content, 55, 110)) -
LENGTH(SUBSTRING_INDEX(SUBSTRING(post_content, 55, 110), ' ', -1))));