Recall & Review
beginner
What does the REPLACE function do in MySQL?
The REPLACE function searches for all occurrences of a substring in a string and replaces them with another substring.
Click to reveal answer
beginner
Syntax of the REPLACE function in MySQL?
REPLACE(original_string, from_substring, to_substring) - It returns a new string where all occurrences of from_substring are replaced by to_substring.
Click to reveal answer
beginner
What happens if the substring to replace is not found in the original string?
The REPLACE function returns the original string unchanged if the substring to replace is not found.
Click to reveal answer
beginner
Example: What is the result of REPLACE('apple pie', 'pie', 'tart')?
The result is 'apple tart' because 'pie' is replaced by 'tart'.
Click to reveal answer
intermediate
Can REPLACE function replace multiple occurrences of the substring?
Yes, REPLACE replaces all occurrences of the substring in the original string, not just the first one.
Click to reveal answer
What does REPLACE('hello world', 'world', 'there') return?
✗ Incorrect
REPLACE changes 'world' to 'there', so the result is 'hello there'.
If the substring to replace is not found, what does REPLACE return?
✗ Incorrect
REPLACE returns the original string unchanged if the substring is not found.
Which of these is the correct syntax for REPLACE?
✗ Incorrect
The correct syntax is REPLACE(original_string, from_substring, to_substring).
What will REPLACE('banana', 'a', 'o') return?
✗ Incorrect
All 'a's are replaced by 'o's, so 'banana' becomes 'bonono'.
Can REPLACE be used to remove a substring by replacing it with an empty string?
✗ Incorrect
Replacing a substring with an empty string effectively removes it.
Explain how the REPLACE function works in MySQL and give a simple example.
Think about changing parts of a word to another word.
You got /4 concepts.
Describe what happens if the substring to replace does not exist in the original string when using REPLACE.
What if you try to replace something that isn't there?
You got /3 concepts.