Skip to content

Instantly share code, notes, and snippets.

@Philosoft
Last active March 28, 2017 06:10
Show Gist options
  • Save Philosoft/41a30fe00ca34757f4349411e5e05a2a to your computer and use it in GitHub Desktop.
Save Philosoft/41a30fe00ca34757f4349411e5e05a2a to your computer and use it in GitHub Desktop.
replace first occurence of substring in string mysql
SET @string = 'this is TEST string with AA replace and An Answer';
SELECT @string as actual_string,
CONCAT(
REPLACE(
LEFT(
@string,
INSTR(@string, 'TEST') + LENGTH('TEST') - 1
),
'TEST',
'WTF'
),
SUBSTRING(
@string,
INSTR(@string, 'TEST') + LENGTH('TEST')
)
) as new_string
@Philosoft
Copy link
Author

Philosoft commented Mar 28, 2017

based on http://stackoverflow.com/a/12123560.

now it can correctly replace occurences longer then 1 char

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment