DEV Community

Cover image for Popular string functions in MySQL - Replace and Reverse
Jo
Jo

Posted on

Popular string functions in MySQL - Replace and Reverse

REPLACE

The replace string function replaces a part of a string with another string. This function is case sensitive.

select replace('hello world', 'wor', '$$$');
Enter fullscreen mode Exit fullscreen mode

Can you guess what the result of this would be?
Result: hello $$$rld
In the example above, we have replaced 'wor' with '$$$'.
The below screenshot uses replace on the "title" column in the books table and replaces all lower letter e's with the number 3

You can also combine the replace string function with other string functions:

select substring(replace(title, 'e', '3'),1,5) As weird_title from books;
Enter fullscreen mode Exit fullscreen mode

The screenshot below shows the results: All the lowercase e's have been replaced by the number 3 and the substring function has been used to extrapolate the first 5 characters from the data.

REVERSE

The reverse string accepts one argument (the string to be reversed) and the reverses it.

select reverse('hello world');
Enter fullscreen mode Exit fullscreen mode

Result is: dlrow olleh

That's the end of this article. Hope you found it useful!

Top comments (0)