- Categories:
String & binary functions (Matching/Comparison)
REPLACE¶
Removes all occurrences of a specified substring, and optionally replaces them with another substring.
Syntax¶
REPLACE( <subject> , <pattern> [ , <replacement> ] )
Arguments¶
subjectThe subject is the string in which to do the replacements. Typically, this is a column, but it can be a literal.
patternThis is the substring that you want to replace. Typically, this is a literal, but it can be a column or expression. Note that this is not a “regular expression”; if you want to use regular expressions to search for a pattern, use the REGEXP_REPLACE function.
replacementThis is the value used as a replacement for the
pattern. If this is omitted, or is an empty string, then theREPLACEfunction simply deletes all occurrences of thepattern.
Returns¶
The returned value is the string after all replacements have been done.
Usage notes¶
If
replacementis not specified,subjectis returned with all occurrences ofpatternremoved.If
replacementis specified,subjectis returned with all occurrences ofpatternreplaced byreplacement.If any of the arguments is a NULL, the result is also a NULL.
Note
Only occurrences in the original subject are considered. A pattern that occurs in the result is not removed/replaced.
Collation details¶
The collation specifications of all input arguments must be compatible.
This function does not support the following collation specifications:
pi(punctuation-insensitive).cs-ai(case-sensitive, accent-insensitive).
Examples¶
Replace the string down with the string up:
SELECT REPLACE('down', 'down', 'up');+-------------------------------+ | REPLACE('DOWN', 'DOWN', 'UP') | |-------------------------------| | up | +-------------------------------+
Replace the substring Athens in the string Vacation in Athens with the substring
Rome:
SELECT REPLACE('Vacation in Athens', 'Athens', 'Rome');+-------------------------------------------------+ | REPLACE('VACATION IN ATHENS', 'ATHENS', 'ROME') | |-------------------------------------------------| | Vacation in Rome | +-------------------------------------------------+
Replace the substring bc in the string abcd with an empty substring:
SELECT REPLACE('abcd', 'bc');+-----------------------+ | REPLACE('ABCD', 'BC') | |-----------------------| | ad | +-----------------------+
Replace the values in a table with new values.
Create and populate a table:
CREATE OR REPLACE TABLE replace_example( subject VARCHAR(10), pattern VARCHAR(10), replacement VARCHAR(10)); INSERT INTO replace_example VALUES ('old car', 'old car', 'new car'), ('sad face', 'sad', 'happy'), ('snowman', 'snow', 'fire');Replace strings in a value with a specified replacement:
SELECT subject, pattern, replacement, REPLACE(subject, pattern, replacement) AS new FROM replace_example ORDER BY subject;+----------+---------+-------------+------------+ | SUBJECT | PATTERN | REPLACEMENT | NEW | |----------+---------+-------------+------------| | old car | old car | new car | new car | | sad face | sad | happy | happy face | | snowman | snow | fire | fireman | +----------+---------+-------------+------------+The output shows the following replacements:
The string
old carwas replaced by the stringnew car.In the string
sad face, the substringsadwas replaced by the substringhappyto create the new stringhappy face.In the string
snowman, the substringsnowwas replaced by the substringfireto create the new stringfireman.