Use the MySQL string function REPLACE() to replace occurrences of a specified string. It will return the original string with all occurrences of the search string replaced by the new string. This is how to replace a string in MySQL.
Requirements:
- MySQL
The objective is to replace all spaces with a dash from all the employees first names.
SELECT
REPLACE(
first_name, ' ', '-'
) AS dashed_first_name
FROM employees
;
Where.
- first_name is the subject string, this where the search will be performed.
- ‘ ‘ is the search string, this is what the function will be looking into.
- ‘-‘ is the replacement for the search string.
- dashed_first_name is just a temporary COLUMN NAME to represent the result.
- employees is the name of the TABLE.
Result.
Before.
After.
Notes:
- The REPLACE() function performs a case-sensitive match when searching for search string.
- It will return NULL if any of its arguments are NULL.
Leave a Reply