// strings reference

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.

Category
Strings
Returns
string
In this category
14 of 172

Syntax

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.

Engine support

EngineSupportNotes
SQL Server Yes SQL Server 2025 and Azure SQL; earlier versions have no regex functions
PostgreSQL Yes Supported under this name.
MySQL Yes 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 Yes Supported under this name.

Examples

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;

Good to know

  • 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.

// fewer lookups

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.