Skip to content

Instantly share code, notes, and snippets.

@thomasplevy
Created October 8, 2014 17:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thomasplevy/c34ca1252cb2ed79829f to your computer and use it in GitHub Desktop.
Save thomasplevy/c34ca1252cb2ed79829f to your computer and use it in GitHub Desktop.
MySQL Database Find and Replace
###
# @source http://stackoverflow.com/a/19374770/400568
###
SELECT t,
INSERT(col,
LOCATE(@start, col),
LOCATE(@end, col) + CHAR_LENGTH(@end) - LOCATE(@start, col),
'') with_delimiters_replaced,
INSERT(col,
LOCATE(@start, col) + CHAR_LENGTH(@start),
LOCATE(@end, col) - LOCATE(@start, col) - CHAR_LENGTH(@start),
'') without_delimiters_replaced
FROM wp_posts.col, (SELECT @start := 'CHANGE:START', @end := 'CHANGE:END') init;
UPDATE t, (SELECT @start := 'CHANGE:START', @end := 'CHANGE:END') init
SET col = INSERT(col,
LOCATE(@start, col),
LOCATE(@end, col) + CHAR_LENGTH(@end) - LOCATE(@start, col),
'');
UPDATE wp_posts, (SELECT @start := 'CHANGE:START', @end := 'CHANGE:END') init
SET col = INSERT(col,
LOCATE(@start, col),
LOCATE(@end, col) + CHAR_LENGTH(@end) - LOCATE(@start, col),
'');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment