Created
April 4, 2018 15:06
-
-
Save h8every1/710913ba4b8e95ccc361f1f4588b6aa7 to your computer and use it in GitHub Desktop.
MySQL - replace first occurrence of string in column
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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