// strings reference

SQL REGEXP_SPLIT_TO_TABLE function

Splits a string wherever a regular expression matches and returns the pieces as a set of rows.

Catégorie
Strings
Returns
set of text rows
In this category
108 of 172

Syntaxe

regexp_split_to_table(string, pattern [, flags])

Prise en charge des moteurs

MoteurSupportNotes
SQL Server Oui SQL Server 2025 and Azure SQL; returns value and ordinal columns.
PostgreSQL Oui Supported under this name.
MySQL Not built in No built-in; recursive CTEs or JSON_TABLE fill the gap.
SQLite Not built in No built-in split; json_each over a JSON array.
Oracle Not built in No built-in; REGEXP_SUBSTR with CONNECT BY LEVEL.

Exemples

PostgreSQL

SELECT regexp_split_to_table('one,two,three', ',') AS word;

PostgreSQL

SELECT l.id, w.word, w.n
FROM logs l, regexp_split_to_table(l.message, '[[:space:]]+') WITH ORDINALITY AS w(word, n);

SQL Server

SELECT value, ordinal FROM REGEXP_SPLIT_TO_TABLE('a1b22c', '[0-9]+');

Bon à savoir

  • To keep each piece's position in PostgreSQL, add WITH ORDINALITY in the FROM clause: regexp_split_to_table(s, ',') WITH ORDINALITY AS t(word, n). SQL Server's version returns an ordinal column by itself.
  • Like regexp_split_to_array, zero-length matches split between characters, and adjacent delimiters produce empty strings.
  • Call it in the FROM clause (as a lateral function) when you need other columns next to the pieces; in the SELECT list it multiplies rows less obviously.

Questions about REGEXP_SPLIT_TO_TABLE

How do I keep track of each element's position?

In PostgreSQL: FROM regexp_split_to_table(str, pat) WITH ORDINALITY AS t(v, n); n is 1-based. In SQL Server 2025, read the ordinal column.

What is the difference between the array and table variants?

regexp_split_to_array returns one array value per input row; regexp_split_to_table returns one row per piece, which is what joins and GROUP BY need.

Browse every function in the SQL function reference, or see what the SQLly desktop app does for your database.

Cheat sheets for the engines that have it: SQL Server, PostgreSQL.

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