Skip to content

Instantly share code, notes, and snippets.

@h8every1
Created April 4, 2018 15:06
Show Gist options
  • Save h8every1/710913ba4b8e95ccc361f1f4588b6aa7 to your computer and use it in GitHub Desktop.
Save h8every1/710913ba4b8e95ccc361f1f4588b6aa7 to your computer and use it in GitHub Desktop.
MySQL - replace first occurrence of string in column
set @oldString = 'http://'; -- string to replace
set @newString = ''; -- string to replace with
UPDATE table_name
set column_name =
IF(
INSTR(column_name, @oldString) <> 0,
CONCAT(
REPLACE(
LEFT(column_name, INSTR(column_name, @oldString) - 1), @oldString, @newString
),
SUBSTRING(column_name, INSTR(column_name, @oldString) + LENGTH(@oldString))
),
column_name
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment