// dialect comparison

Regular expression replace in SQL Server, PostgreSQL, MySQL, SQLite and Oracle

PostgreSQL, MySQL and Oracle have had it for years, SQL Server added it in 2025, and SQLite's core still has none.

Filed under Querying

The verdict

The function name is the same in four engines, but the default differs: PostgreSQL replaces the first match unless you pass 'g', while MySQL, Oracle and SQL Server replace all. SQLite is the holdout, so keep regex cleanup in the app for engine-neutral code.

Where they differ

Regex replace fixes dirty data in one line if your engine has it. PostgreSQL, MySQL 8 and Oracle do, SQL Server gained it in SQL Server 2025 and Azure SQL, and SQLite's core library has none. Each row replaces digit runs in 'abc123def45'.

How each engine writes it

SQL Server

SELECT REGEXP_REPLACE('abc123def45', '[0-9]+', '#');

SQL Server 2025 and Azure SQL. Replaces every match by default (abc#def#). REGEXP_REPLACE works at any compatibility level, while REGEXP_LIKE needs level 170. Older releases need PATINDEX tricks or a CLR function.

PostgreSQL

SELECT REGEXP_REPLACE('abc123def45', '[0-9]+', '#', 'g');

Without the 'g' flag only the first match is replaced (abc#def45); with it you get abc#def#. PostgreSQL uses its own advanced regex flavor, which supports lookahead.

MySQL

SELECT REGEXP_REPLACE('abc123def45', '[0-9]+', '#');

MySQL 8.0.4+ with the ICU regex library; replaces every occurrence by default (abc#def#). Earlier versions had REGEXP matching but no replace.

SQLite

SELECT REPLACE('abc123def45', '123', '#');

The core library has no regex functions. X REGEXP Y calls a user function named regexp that nobody defines by default (the sqlite3 shell loads one), and there is no REGEXP_REPLACE. Load an extension, register a function from your driver, or fall back to literal REPLACE as here, which only handles a known string: this returns abc#def45, not abc#def#.

Oracle

SELECT REGEXP_REPLACE('abc123def45', '[0-9]+', '#') FROM dual;

POSIX regex with Perl-style shortcuts such as \d, since 10g. Replaces every match by default (abc#def#), with position and occurrence arguments for finer control.

Questions that come up

Why does PostgreSQL replace only the first match?

REGEXP_REPLACE defaults to one substitution; add the flag argument: REGEXP_REPLACE(s, '[0-9]+', '#', 'g'). MySQL, Oracle and SQL Server replace every match by default, so SQL ported to PostgreSQL must add the 'g'.

Which regex flavor is which?

PostgreSQL uses its own advanced regex engine and MySQL uses ICU; both support lookahead. Oracle is POSIX ERE with Perl-style shortcuts like \d, SQL Server 2025 uses RE2 (no lookaround or backreferences), and SQLite extensions vary. Anchors and character classes port; lookaround does not.

What is the pre-2025 answer on SQL Server?

Compose PATINDEX + STUFF for single simple patterns, push the cleanup to the app, or use a SQL CLR regex function. All are clunky enough that most teams did this cleanup outside the database.

Keep reading

// write it once, run it anywhere

Switch engines without relearning SQL

SQLly is a native SQL workbench for macOS, Windows and Linux. Its formatter and plan viewer work in all five dialects.