DEV Community

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

Posted on

2 2

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!

Speedy emails, satisfied customers

Postmark Image

Are delayed transactional emails costing you user satisfaction? Postmark delivers your emails almost instantly, keeping your customers happy and connected.

Sign up

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay