SQL REGEXP_REPLACE function
Replaces text matching a regular expression. The engines that have it disagree on whether it replaces the first match or all of them, and on how to write a capture group.
- 类别
- Strings
- Returns
string- In this category
- 14 of 172
语法
REGEXP_REPLACE(string, pattern, replacement [, flags]) -- PostgreSQL
REGEXP_REPLACE(string, pattern, replacement [, start [, occurrence [, flags]]])
Each block is a separate form. The support table and the examples show which engine takes which.
引擎支持
| 引擎 | Support | 备注 |
|---|---|---|
| SQL Server | 是 | SQL Server 2025 and Azure SQL; earlier versions have no regex functions |
| PostgreSQL | 是 | Supported under this name. |
| MySQL | 是 | MySQL 8.0+, using the ICU regex library |
| SQLite | Not built in | no REGEXP_REPLACE; even the REGEXP operator needs a user function or extension |
| Oracle | 是 | Supported under this name. |
示例
PostgreSQL
SELECT REGEXP_REPLACE(phone, '[^0-9]', '', 'g') AS digits FROM contacts;
Oracle
-- replaces every match by default SELECT REGEXP_REPLACE(phone, '[^0-9]', '') AS digits FROM contacts;
MySQL
-- start 1, occurrence 0 (all), case-insensitive SELECT REGEXP_REPLACE(note, '<br[[:space:]]*/?>', ' ', 1, 0, 'i') AS clean FROM notes;
SQL Server
-- SQL Server 2025 SELECT REGEXP_REPLACE(phone, '[^0-9]', '') AS digits FROM contacts;
值得了解
- First match or all: PostgreSQL replaces only the first match unless you pass the 'g' flag. MySQL, Oracle and SQL Server 2025 replace every match by default (occurrence 0).
- Capture groups in the replacement are \1 on PostgreSQL, Oracle and SQL Server, but $1 on MySQL: REGEXP_REPLACE('abc', '(b)', '[$1]') is 'a[b]c'.
- Case-insensitive matching: the 'i' flag. PostgreSQL takes it as the fourth argument; MySQL, Oracle and SQL Server take it as the sixth, after start position and occurrence.
- On MySQL a backslash in a string literal is itself an escape character, so a regex \s must be written '\\s'. POSIX classes such as [[:space:]] avoid the problem.
Questions about REGEXP_REPLACE
Does SQL Server have REGEXP_REPLACE?
From SQL Server 2025 (and in Azure SQL Database), yes, along with REGEXP_LIKE, REGEXP_SUBSTR and the other REGEXP_ functions. Earlier versions have only LIKE and PATINDEX wildcards, so nested REPLACE or a SQL CLR function is the workaround.
How do I make REGEXP_REPLACE case-insensitive?
Pass the 'i' flag. On PostgreSQL it is the fourth argument: REGEXP_REPLACE(s, 'x', '-', 'gi'). On MySQL, Oracle and SQL Server 2025 it is the sixth: REGEXP_REPLACE(s, 'x', '-', 1, 0, 'i'). MySQL also accepts (?i) at the start of the pattern.
Browse every function in the SQL function reference, or see what the SQLly desktop app does for your database.
Related on SQLly
- Regular expression replace in SQL Server, PostgreSQL, MySQL, SQLite and Oracle Dialect comparison
- Remove emojis and symbol characters in SQL Snippet
- Normalize whitespace in SQL Snippet
- Remove non-printable and control characters in SQL Snippet
- Find duplicates hiding behind whitespace Snippet
- Validate phone number shape Snippet
Cheat sheets for the engines that have it: SQL Server, PostgreSQL, MySQL, Oracle.
Functions your engine actually has
SQLly's completion lists the built-in functions of the engine you're connected to, whether that's SQL Server, PostgreSQL, MySQL, SQLite or Oracle. Hover a function for help, and signature hints show each argument as you type.