REGEXP_REPLACE
functionsupportedCore SQL functionsReturns the subject with the specified pattern — or all occurrences of the pattern — either removed or replaced by a replacement string.
Syntax
supportedpartialnot supportednot scored
REGEXP_REPLACE( <subject> ,
<pattern>
[ , <replacement>
[ , <position>
[ , <occurrence>
[ , <parameters> ]
]
]
]
)
Parameters
| Name | Required | Description |
|---|---|---|
subject | required | The string to search for matches. |
pattern | required | Pattern to match. For guidelines on specifying patterns, see String functions (regular expressions). |
replacement | optional | String that replaces the substrings matched by the pattern. If an empty string is specified, the function removes all matched patterns and returns the resulting string. Default: '' (empty string). |
position | optional | Number of characters from the beginning of the string where the function starts searching for matches. The value must be a positive integer. Default: 1 (the search for a match starts at the first character on the left) |
occurrence | optional | Specifies which occurrence of the pattern to replace. If 0 is specified, all occurrences are replaced. Default: 0 (all occurrences) |
Returns
Returns a value of type VARCHAR. When the 2026_04 BCR bundle is enabled and the subject is collated, the subject’s collation propagates to the return value; apply COLLATE '' to the subject to get an uncollated result. If no matches are found, returns the original subject. Returns NULL if any argument is NULL.
Usage notes
- The replacement string can contain backreferences to capture groups; for example, sub-expressions of the pattern. A capture group is a regular expression that is enclosed within parentheses (( )). The maximum number of capture groups is nine. Backreferences match expressions inside a capture group. Backreferences have the form n where n is a value from 0 to 9, inclusive, which refers to the matching instance of the capture group. For more information, see Examples (in this topic).
- Parentheses (( )) and square brackets ([ ]) currently must be double-escaped to parse them as literal strings. The example below shows how to remove parentheses: Copy code SELECT REGEXP_REPLACE('Customers - (NY)','\\(|\\)','') AS customers; +----------------+ | CUSTOMERS | |----------------| | Customers - NY | +----------------+
- For additional usage notes, see the General usage notes for regular expression functions.
Collation details
When the 2026_04 BCR bundle is enabled, the function accepts arguments with a collation specification. The collation has no effect on pattern matching: matching is always case-sensitive unless you pass the i flag in the parameters argument.