The REPLACE function returns a string in which all occurrences of a specifiedmatch string are replaced with a replacement string. REPLACE is useful for searching a pattern of characters, and
then changing all instances of that pattern in a single function call. The specification of the REPLACE function is:
FUNCTION REPLACE (string1 IN VARCHAR2, match_string IN VARCHAR2
[, replace_string IN VARCHAR2]) RETURN VARCHAR2
If you do not specify the replacement string, then REPLACE simply removes all occurrences of the match_string in string1. If you specify neither a match string nor a replacement string, REPLACE returns NULL.
Here are several examples using REPLACE:
• Remove all instances of the letter "C" in the string "CAT CALL":
REPLACE ('CAT CALL', 'C') --> 'AT ALL'
Because we did not specify a replacement string, REPLACE changed all occurrences of
"C" to NULL.
• Replace all occurrences of "99" with "100" in the following string:
• REPLACE ('Zero defects in period 99 reached 99%!', '99', '100')
• -->
'Zero defects in period 100 reached 100%!'
• Handle occurrences of a single quote within a query criteria string. The single quote is a string terminator symbol, indicating the start and/or end of the literal string. I once ran into this requirement when building query-by-example strings in Oracle Forms. If the user enters a string with a single quote in it, such as:
Customer didn't have change.
and then the program concatenates that string into a larger string, the resulting SQL statement (created dynamically by Oracle Forms in Query Mode) fails because there are unbalanced single quotes in the string. You can resolve this problem by converting that single quote into two single quotes in a row, ther eby telling SQL that you really intend for one quote to be part of the string. Use the following REPLACE to do this:
criteria_string := REPLACE (criteria_string, '''', '''''');
The four quotes in the second parameter resolve to a string containing one single quote.
The six quotes in the third parameter resolve to a string containing two single quotes. In each case, the outer pair of quotes delimits the string, and each pair of single quotes inside that outer pair are interpreted as just one single quote within the string.
• Remove all leading instances of "abc" from the string:
"abcabcccccI LOVE CHILIabc"
This is the behavior we were looking at in the previous section on LTRIM. We want to remove all instances of the pattern "abc" from the beginning of the string, but we do not
want to remove that pattern throughout the rest of the string. In addition, we want to remove "abc" only as a pattern; if we encounter three contiguous c's ("ccc"), on the other hand, they should not be removed from the string. This task is less straightforward than it might at first seem. If we simply apply REPLACE to the string, it will remove all occurrences of "abc", instead of just the leading instances. For example:
REPLACE ('abcabccccI LOVE CHILIabc', 'abc') --> 'cccI LOVE CHILI'
That is not what we want in this case. If we use LTRIM, on the other hand, we will be left with none of the leading c's, as demonstrated in a previous example:
LTRIM ('abcabcccccI LOVE CHILIabc', 'abc') --> 'I LOVE CHILIabc'
And this is not quite right either. We want to be left with "cccI LOVE CHILIabc" (please do not ask why), and it turns out that the way to get it is to use a combination of LTRIM and REPLACE. Suppose that we create a local variable as follows:
my_string := 'abcabccccI LOVE CHILIabc';
Then the following statement will achieve the desired effect by nesting calls to REPLACE and LTRIM within one another:
REPLACE (LTRIM
(REPLACE (my_string, 'abc', '@'), '@'), '@', 'abc')
-->
'cccI LOVE CHILIabc'
Here is how we would describe in English what the above statement does:
First replace all occurrences of "abc" with the special character "@" (which we are assuming does not otherwise appear in the string). Then trim off all leading instances of
"@". Finally, replace all remaining occurrences of "@" with "abc".
Voilà, as they say in many of the finer restaurants in Paris. Now let's pull apart this single, rather complex statement into separate PL/SQL steps corresponding to the "natural language" description:
Replace all occurrences of "abc" with the special character "@" (which we are assuming does not otherwise appear in the string). Notice that this only affects the "abc" pattern, and not any individual appearances of "a", "b", or "c".
REPLACE ('abcabccccI LOVE CHILIabc', 'abc', '@') --> '@@cccI LOVE CHILI@'
Trim off all leading instances of "@".
LTRIM ('@@cccI LOVE CHILI@', '@') --> 'cccI LOVE CHILI@'
Notice that LTRIM now leaves the c's in place, because we didn't ask it to remove "a" or
"b" or "c"—just "@". In addition, it left the trailing "@" in the string because LTRIM deals only with characters on the leading end of the string.
Replace all remaining occurrences of "@" with "abc".
REPLACE ('cccI LOVE CHILI@', '@', 'abc') --> 'cccI LOVE CHILIabc'
And we are done. We used the first REPLACE to temporarily change the occurrences of
"abc" so that LTRIM could distinguish between the pattern we wanted to get rid of and the extra characters that needed to be preserved. Then a final call to REPLACE restored the pattern in the string.