{"id":2139,"date":"2025-02-17T11:50:03","date_gmt":"2025-02-17T04:50:03","guid":{"rendered":"https:\/\/www.pgtutorial.com\/?page_id=2139"},"modified":"2025-02-17T13:18:49","modified_gmt":"2025-02-17T06:18:49","slug":"postgresql-regexp_split_to_table","status":"publish","type":"page","link":"https:\/\/www.pgtutorial.com\/postgresql-string-functions\/postgresql-regexp_split_to_table\/","title":{"rendered":"PostgreSQL REGEXP_SPLIT_TO_TABLE Function"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: In this tutorial, you&#8217;ll learn how to use the PostgreSQL <code>REGEXP_SPLIT_TO_TABLE<\/code> function to split a string into one or more rows based on a regular expression pattern.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='introduction-to-regexp_split_to_table-function'>Introduction to REGEXP_SPLIT_TO_TABLE Function <a href=\"#introduction-to-regexp_split_to_table-function\" class=\"anchor\" id=\"introduction-to-regexp_split_to_table-function\" title=\"Anchor for Introduction to REGEXP_SPLIT_TO_TABLE Function\">#<\/a><\/h2>\n\n\n\n<p>The <code>REGEXP_SPLIT_TO_TABLE<\/code> function splits a string into multiple rows based on a <a href=\"https:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-regular-expressions\/#introduction-to-posix-regular-expressions\" target=\"_blank\" rel=\"noreferrer noopener\">POSIX regular expression pattern<\/a> as a delimiter.<\/p>\n\n\n\n<p>Here&#8217;s the syntax of the <code>REGEXP_SPLIT_TO_TABLE<\/code> function:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"PostgreSQL SQL dialect and PL\/pgSQL\" data-shcb-language-slug=\"pgsql\"><span><code class=\"hljs language-pgsql\">REGEXP_SPLIT_TO_TABLE(string <span class=\"hljs-type\">text<\/span>, pattern <span class=\"hljs-type\">text<\/span>) -&gt; <span class=\"hljs-keyword\">SETOF<\/span> <span class=\"hljs-type\">text<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PostgreSQL SQL dialect and PL\/pgSQL<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">pgsql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Parameters:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>string<\/code>: The input string you want to split.<\/li>\n\n\n\n<li><code>pattern<\/code>: A POSIX regular expression pattern used as a delimiter for splitting the <code>source<\/code> string.<\/li>\n\n\n\n<li><code>flags<\/code> (optional): Modifiers that change how the regular expression matches the <code>source<\/code> string.<\/li>\n<\/ul>\n\n\n\n<p>The <code>REGEXP_SPLIT_TO_TABLE<\/code> function returns a set of text values, where each row represents a substring.<\/p>\n\n\n\n<p>If the <code>REGEXP_SPLIT_TO_TABLE<\/code> function does not find a match for the given regular expression pattern in the input string, it returns the entire input string as a single row.<\/p>\n\n\n\n<p>The <code>REGEXP_SPLIT_TO_TABLE<\/code> function returns an empty set (no rows) if the input string or regular expression pattern is <code>NULL<\/code>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='basic-postgresql-regexp_split_to_table-function-examples'>Basic PostgreSQL REGEXP_SPLIT_TO_TABLE Function Examples <a href=\"#basic-postgresql-regexp_split_to_table-function-examples\" class=\"anchor\" id=\"basic-postgresql-regexp_split_to_table-function-examples\" title=\"Anchor for Basic PostgreSQL REGEXP_SPLIT_TO_TABLE Function Examples\">#<\/a><\/h2>\n\n\n\n<h3 class=\"wp-block-heading\" id='example-1-split-a-string-using-a-comma-as-the-delimiter'>Example 1: Split a string using a comma (&#8220;,&#8221;) as the delimiter <a href=\"#example-1-split-a-string-using-a-comma-as-the-delimiter\" class=\"anchor\" id=\"example-1-split-a-string-using-a-comma-as-the-delimiter\" title=\"Anchor for Example 1: Split a string using a comma (&quot;,&quot;) as the delimiter\">#<\/a><\/h3>\n\n\n\n<p>The following example uses the <code>REGEXP_SPLIT_TO_TABLE<\/code> function to split a string using a comma (<code>\",\"<\/code>) as the delimiter:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"PostgreSQL SQL dialect and PL\/pgSQL\" data-shcb-language-slug=\"pgsql\"><span><code class=\"hljs language-pgsql\"><span class=\"hljs-keyword\">SELECT<\/span>\n  REGEXP_SPLIT_TO_TABLE(<span class=\"hljs-string\">'SQL,Postgres,PostgreSQL'<\/span>, <span class=\"hljs-string\">','<\/span>) <span class=\"hljs-keyword\">AS<\/span> result;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PostgreSQL SQL dialect and PL\/pgSQL<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">pgsql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a href=\"https:\/\/www.pgtutorial.com\/playground\/?q=U0VMRUNUIFJFR0VYUF9TUExJVF9UT19UQUJMRSgnU1FMLFBvc3RncmVzLFBvc3RncmVTUUwnLCAnLCcpIEFTIHJlc3VsdDs%3D\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"PostgreSQL SQL dialect and PL\/pgSQL\" data-shcb-language-slug=\"pgsql\"><span><code class=\"hljs language-pgsql\">   result\n<span class=\"hljs-comment\">------------<\/span>\n <span class=\"hljs-keyword\">SQL<\/span>\n Postgres\n PostgreSQL<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PostgreSQL SQL dialect and PL\/pgSQL<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">pgsql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h3 class=\"wp-block-heading\" id='example-2-split-a-string-using-one-or-more-digits-as-a-delimiter'>Example 2: Split a string using one or more digits as a delimiter <a href=\"#example-2-split-a-string-using-one-or-more-digits-as-a-delimiter\" class=\"anchor\" id=\"example-2-split-a-string-using-one-or-more-digits-as-a-delimiter\" title=\"Anchor for Example 2: Split a string using one or more digits as a delimiter\">#<\/a><\/h3>\n\n\n\n<p>The following query uses the <code>REGEXP_SPLIT_TO_TABLE<\/code> function to split a string using one or more digits as a delimiter:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"PostgreSQL SQL dialect and PL\/pgSQL\" data-shcb-language-slug=\"pgsql\"><span><code class=\"hljs language-pgsql\"><span class=\"hljs-keyword\">SELECT<\/span>\n  s\n<span class=\"hljs-keyword\">FROM<\/span>\n  REGEXP_SPLIT_TO_TABLE(<span class=\"hljs-string\">'PostgreSQL 17 Live'<\/span>, <span class=\"hljs-string\">'\\d+'<\/span>) s;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PostgreSQL SQL dialect and PL\/pgSQL<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">pgsql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a href=\"https:\/\/www.pgtutorial.com\/playground\/?q=U0VMRUNUIHMgRlJPTSBSRUdFWFBfU1BMSVRfVE9fVEFCTEUoJ1Bvc3RncmVTUUwgMTcgTGl2ZScsICdcZCsnKSBzOw%3D%3D\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-5\" data-shcb-language-name=\"PostgreSQL SQL dialect and PL\/pgSQL\" data-shcb-language-slug=\"pgsql\"><span><code class=\"hljs language-pgsql\">      s\n<span class=\"hljs-comment\">-------------<\/span>\n PostgreSQL\n  Live\n(<span class=\"hljs-number\">2<\/span> <span class=\"hljs-keyword\">rows<\/span>)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PostgreSQL SQL dialect and PL\/pgSQL<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">pgsql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>In this example, we use the <code>REGEXP_SPLIT_TO_TABLE<\/code> function in the <code>FROM<\/code> clause. The query returns rows produced by the <code>REGEXP_SPLIT_TO_TABLE<\/code> function.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id='example-3-no-match-found'>Example 3: No match found <a href=\"#example-3-no-match-found\" class=\"anchor\" id=\"example-3-no-match-found\" title=\"Anchor for Example 3: No match found\">#<\/a><\/h3>\n\n\n\n<p>The following example uses the <code>REGEXP_SPLIT_TO_TABLE<\/code> function to return the entire input string as a row of the result because there is no match:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-6\" data-shcb-language-name=\"PostgreSQL SQL dialect and PL\/pgSQL\" data-shcb-language-slug=\"pgsql\"><span><code class=\"hljs language-pgsql\"><span class=\"hljs-keyword\">SELECT<\/span>\n  REGEXP_SPLIT_TO_TABLE(<span class=\"hljs-string\">'pgtutorial.com'<\/span>, <span class=\"hljs-string\">'\\d+'<\/span>) <span class=\"hljs-keyword\">AS<\/span> result;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-6\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PostgreSQL SQL dialect and PL\/pgSQL<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">pgsql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-7\" data-shcb-language-name=\"PostgreSQL SQL dialect and PL\/pgSQL\" data-shcb-language-slug=\"pgsql\"><span><code class=\"hljs language-pgsql\">     result\n<span class=\"hljs-comment\">----------------<\/span>\n pgtutorial.com<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-7\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PostgreSQL SQL dialect and PL\/pgSQL<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">pgsql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h2 class=\"wp-block-heading\" id='using-whitespace-as-a-delimiter'>Using Whitespace as a Delimiter <a href=\"#using-whitespace-as-a-delimiter\" class=\"anchor\" id=\"using-whitespace-as-a-delimiter\" title=\"Anchor for Using Whitespace as a Delimiter\">#<\/a><\/h2>\n\n\n\n<p>The following query uses the <code>REGEXP_SPLIT_TO_TABLE<\/code> function to split a string based on whitespace characters using <code>\\s+<\/code> as the pattern:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-8\" data-shcb-language-name=\"PostgreSQL SQL dialect and PL\/pgSQL\" data-shcb-language-slug=\"pgsql\"><span><code class=\"hljs language-pgsql\"><span class=\"hljs-keyword\">SELECT<\/span>\n  REGEXP_SPLIT_TO_TABLE(<span class=\"hljs-string\">'PostgreSQL Tutorial'<\/span>, <span class=\"hljs-string\">'\\s+'<\/span>) <span class=\"hljs-keyword\">AS<\/span> result;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-8\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PostgreSQL SQL dialect and PL\/pgSQL<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">pgsql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-9\" data-shcb-language-name=\"PostgreSQL SQL dialect and PL\/pgSQL\" data-shcb-language-slug=\"pgsql\"><span><code class=\"hljs language-pgsql\">   result\n<span class=\"hljs-comment\">------------<\/span>\n PostgreSQL\n Tutorial<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-9\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PostgreSQL SQL dialect and PL\/pgSQL<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">pgsql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>In this example, the regular expression <code>\\s+<\/code> treats one or more spaces as a single delimiter, ensuring the result string has no empty string.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='splitting-based-on-multiple-delimiters'>Splitting Based on Multiple Delimiters <a href=\"#splitting-based-on-multiple-delimiters\" class=\"anchor\" id=\"splitting-based-on-multiple-delimiters\" title=\"Anchor for Splitting Based on Multiple Delimiters\">#<\/a><\/h2>\n\n\n\n<p>The following example uses the <code>REGEXP_SPLIT_TO_TABLE<\/code> function with a pattern to split a string on multiple delimiters, including commas (&#8220;,&#8221;), semicolons (&#8220;;&#8221;), and spaces (&#8221; &#8220;):<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-10\" data-shcb-language-name=\"PostgreSQL SQL dialect and PL\/pgSQL\" data-shcb-language-slug=\"pgsql\"><span><code class=\"hljs language-pgsql\"><span class=\"hljs-keyword\">SELECT<\/span>\n  REGEXP_SPLIT_TO_TABLE(<span class=\"hljs-string\">'SQL,Postgres,PostgreSQL'<\/span>, <span class=\"hljs-string\">'&#91;,; ]+'<\/span>) <span class=\"hljs-keyword\">AS<\/span> result;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-10\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PostgreSQL SQL dialect and PL\/pgSQL<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">pgsql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-11\" data-shcb-language-name=\"PostgreSQL SQL dialect and PL\/pgSQL\" data-shcb-language-slug=\"pgsql\"><span><code class=\"hljs language-pgsql\">   result\n<span class=\"hljs-comment\">------------<\/span>\n <span class=\"hljs-keyword\">SQL<\/span>\n Postgres\n PostgreSQL<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-11\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PostgreSQL SQL dialect and PL\/pgSQL<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">pgsql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h2 class=\"wp-block-heading\" id='handling-special-characters'>Handling Special Characters <a href=\"#handling-special-characters\" class=\"anchor\" id=\"handling-special-characters\" title=\"Anchor for Handling Special Characters\">#<\/a><\/h2>\n\n\n\n<p>If the input string contains special regular expression characters, you must escape it properly.<\/p>\n\n\n\n<p>For example, the following statement uses the <code>REGEXP_SPLIT_TO_TABLE<\/code> function to split the domain name (www.pgtutorial.com) using the character (.):<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-12\" data-shcb-language-name=\"PostgreSQL SQL dialect and PL\/pgSQL\" data-shcb-language-slug=\"pgsql\"><span><code class=\"hljs language-pgsql\"><span class=\"hljs-keyword\">SELECT<\/span>\n  REGEXP_SPLIT_TO_TABLE(<span class=\"hljs-string\">'www.pgtutorial.com'<\/span>, <span class=\"hljs-string\">'\\.'<\/span>) <span class=\"hljs-keyword\">AS<\/span> result;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-12\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PostgreSQL SQL dialect and PL\/pgSQL<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">pgsql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a href=\"https:\/\/www.pgtutorial.com\/playground\/?q=U0VMRUNUIFJFR0VYUF9TUExJVF9UT19UQUJMRSgnd3d3LnBndHV0b3JpYWwuY29tJywgJ1wuJykgQVMgcmVzdWx0Ow%3D%3D\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-13\" data-shcb-language-name=\"PostgreSQL SQL dialect and PL\/pgSQL\" data-shcb-language-slug=\"pgsql\"><span><code class=\"hljs language-pgsql\">   result\n<span class=\"hljs-comment\">------------<\/span>\n www\n pgtutorial\n com<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-13\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PostgreSQL SQL dialect and PL\/pgSQL<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">pgsql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Since the <code>.<\/code> is a special regular expression character, we escape it using a backslash (<code>\\.<\/code>).<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='using-regexp_split_to_table-function-with-table-data'>Using REGEXP_SPLIT_TO_TABLE Function with Table Data <a href=\"#using-regexp_split_to_table-function-with-table-data\" class=\"anchor\" id=\"using-regexp_split_to_table-function-with-table-data\" title=\"Anchor for Using REGEXP_SPLIT_TO_TABLE Function with Table Data\">#<\/a><\/h2>\n\n\n\n<p>Step 1: <a href=\"https:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-create-table\/\">Create a table<\/a> called <code>posts<\/code>:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-14\" data-shcb-language-name=\"PostgreSQL SQL dialect and PL\/pgSQL\" data-shcb-language-slug=\"pgsql\"><span><code class=\"hljs language-pgsql\"><span class=\"hljs-keyword\">CREATE<\/span> <span class=\"hljs-keyword\">TABLE<\/span> posts (\n    id <span class=\"hljs-type\">INT<\/span> <span class=\"hljs-keyword\">GENERATED<\/span> <span class=\"hljs-keyword\">ALWAYS<\/span> <span class=\"hljs-keyword\">AS<\/span> <span class=\"hljs-keyword\">IDENTITY<\/span> <span class=\"hljs-keyword\">PRIMARY KEY<\/span>,\n    body <span class=\"hljs-type\">VARCHAR<\/span>(<span class=\"hljs-number\">200<\/span>) <span class=\"hljs-keyword\">NOT<\/span> <span class=\"hljs-keyword\">NULL<\/span>,\n    tags <span class=\"hljs-type\">TEXT<\/span>\n);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-14\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PostgreSQL SQL dialect and PL\/pgSQL<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">pgsql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Step 2: Insert some rows into the <code>posts<\/code> table:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-15\" data-shcb-language-name=\"PostgreSQL SQL dialect and PL\/pgSQL\" data-shcb-language-slug=\"pgsql\"><span><code class=\"hljs language-pgsql\"><span class=\"hljs-keyword\">INSERT<\/span> <span class=\"hljs-keyword\">INTO<\/span> posts (body, tags)\n<span class=\"hljs-keyword\">VALUES<\/span>\n(<span class=\"hljs-string\">'Learn how REGEXP_SPLIT_TO_TABLE function can split strings efficiently'<\/span>, <span class=\"hljs-string\">'#database #postgres #regex'<\/span>),\n(<span class=\"hljs-string\">'Need to split a string into an array in postgres? Use REGEXP_SPLIT_TO_ARRAY!'<\/span>, <span class=\"hljs-string\">'#database #postgres #array'<\/span>);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-15\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PostgreSQL SQL dialect and PL\/pgSQL<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">pgsql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Step 3: Split strings in the <code>tags<\/code> column into multiple rows:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-16\" data-shcb-language-name=\"PostgreSQL SQL dialect and PL\/pgSQL\" data-shcb-language-slug=\"pgsql\"><span><code class=\"hljs language-pgsql\"><span class=\"hljs-keyword\">SELECT<\/span>\n  id,\n  TRIM(tag) <span class=\"hljs-keyword\">AS<\/span> tag\n<span class=\"hljs-keyword\">FROM<\/span>\n  posts,\n  REGEXP_SPLIT_TO_TABLE(tags, <span class=\"hljs-string\">'#'<\/span>) <span class=\"hljs-keyword\">AS<\/span> tag\n<span class=\"hljs-keyword\">WHERE<\/span>\n  TRIM(tag) &lt;&gt; <span class=\"hljs-string\">''<\/span>;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-16\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PostgreSQL SQL dialect and PL\/pgSQL<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">pgsql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-17\" data-shcb-language-name=\"PostgreSQL SQL dialect and PL\/pgSQL\" data-shcb-language-slug=\"pgsql\"><span><code class=\"hljs language-pgsql\">id  | tag\n<span class=\"hljs-comment\">----|--------<\/span>\n<span class=\"hljs-number\">1<\/span>   | <span class=\"hljs-keyword\">database<\/span>\n<span class=\"hljs-number\">1<\/span>   | postgres\n<span class=\"hljs-number\">1<\/span>   | regex\n<span class=\"hljs-number\">2<\/span>   | <span class=\"hljs-keyword\">database<\/span>\n<span class=\"hljs-number\">2<\/span>   | postgres\n<span class=\"hljs-number\">2<\/span>   | <span class=\"hljs-keyword\">array<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-17\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PostgreSQL SQL dialect and PL\/pgSQL<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">pgsql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>How the query works:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The <code>REGEXP_SPLIT_TO_TABLE(tags, '#')<\/code> function splits the <code>tags<\/code> column by <code>#<\/code>.<\/li>\n\n\n\n<li><code>TRIM(tag)<\/code> removes leading and trailing spaces from the resulting tags.<\/li>\n\n\n\n<li>The <code>WHERE TRIM(tag) &lt;&gt; ''<\/code> filter removes empty tags.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id='summary'>Summary <a href=\"#summary\" class=\"anchor\" id=\"summary\" title=\"Anchor for Summary\">#<\/a><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Use the <code>REGEXP_SPLIT_TO_TABLE<\/code> function to split a string into rows based on a POSIX regular expression pattern.<\/li>\n<\/ul>\n<div class=\"helpful-block-content\" data-title=\"\">\n\t<header>\n\t\t<div class=\"wth-question\">Was this tutorial helpful ?<\/div>\n\t\t<div class=\"wth-thumbs\">\n\t\t\t<button\n\t\t\t\tdata-post=\"2139\"\n\t\t\t\tdata-post-url=\"https:\/\/www.pgtutorial.com\/postgresql-string-functions\/postgresql-regexp_split_to_table\/\"\n\t\t\t\tdata-post-title=\"PostgreSQL REGEXP_SPLIT_TO_TABLE Function\"\n\t\t\t\tdata-response=\"1\"\n\t\t\t\tclass=\"wth-btn-rounded wth-yes-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t\tclass=\"feather feather-thumbs-up block w-full h-full\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M14 9V5a3 3 0 0 0-3-3l-4 9v11h11.28a2 2 0 0 0 2-1.7l1.38-9a2 2 0 0 0-2-2.3zM7 22H4a2 2 0 0 1-2-2v-7a2 2 0 0 1 2-2h3\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> Yes <\/span>\n\t\t\t<\/button>\n\n\t\t\t<button\n\t\t\t\tdata-response=\"0\"\n\t\t\t\tdata-post=\"2139\"\n\t\t\t\tdata-post-url=\"https:\/\/www.pgtutorial.com\/postgresql-string-functions\/postgresql-regexp_split_to_table\/\"\n\t\t\t\tdata-post-title=\"PostgreSQL REGEXP_SPLIT_TO_TABLE Function\"\n\t\t\t\tclass=\"wth-btn-rounded wth-no-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M10 15v4a3 3 0 0 0 3 3l4-9V2H5.72a2 2 0 0 0-2 1.7l-1.38 9a2 2 0 0 0 2 2.3zm7-13h2.67A2.31 2.31 0 0 1 22 4v7a2.31 2.31 0 0 1-2.33 2H17\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> No <\/span>\n\t\t\t<\/button>\n\t\t<\/div>\n\t<\/header>\n\n\t<div class=\"wth-form hidden\">\n\t\t<div class=\"wth-form-wrapper\">\n\t\t\t<div class=\"wth-title\"><\/div>\n\t\t\t\n\t\t\t<textarea class=\"wth-message\"><\/textarea>\n\n\t\t\t<button class=\"btn btn-primary wth-btn-submit\">Send<\/button>\n\t\t\t<button class=\"btn wth-btn-cancel\">Cancel<\/button>\n\t\t\n\t\t<\/div>\n\t<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>In this tutorial, you&#8217;ll learn how to use the PostgreSQL REGEXP_SPLIT_TO_TABLE function to split a string into one or more rows based on a regular expression pattern.<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":1800,"menu_order":25,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-2139","page","type-page","status-publish","hentry"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.5 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>PostgreSQL REGEXP_SPLIT_TO_TABLE Function<\/title>\n<meta name=\"description\" content=\"Learn how to use the PostgreSQL REGEXP_SPLIT_TO_TABLE function to split a string into multiple rows based on a regular expression pattern.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.pgtutorial.com\/postgresql-string-functions\/postgresql-regexp_split_to_table\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"PostgreSQL REGEXP_SPLIT_TO_TABLE Function\" \/>\n<meta property=\"og:description\" content=\"Learn how to use the PostgreSQL REGEXP_SPLIT_TO_TABLE function to split a string into multiple rows based on a regular expression pattern.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.pgtutorial.com\/postgresql-string-functions\/postgresql-regexp_split_to_table\/\" \/>\n<meta property=\"og:site_name\" content=\"PostgreSQL Tutorial\" \/>\n<meta property=\"article:modified_time\" content=\"2025-02-17T06:18:49+00:00\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.pgtutorial.com\\\/postgresql-string-functions\\\/postgresql-regexp_split_to_table\\\/\",\"url\":\"https:\\\/\\\/www.pgtutorial.com\\\/postgresql-string-functions\\\/postgresql-regexp_split_to_table\\\/\",\"name\":\"PostgreSQL REGEXP_SPLIT_TO_TABLE Function\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.pgtutorial.com\\\/#website\"},\"datePublished\":\"2025-02-17T04:50:03+00:00\",\"dateModified\":\"2025-02-17T06:18:49+00:00\",\"description\":\"Learn how to use the PostgreSQL REGEXP_SPLIT_TO_TABLE function to split a string into multiple rows based on a regular expression pattern.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.pgtutorial.com\\\/postgresql-string-functions\\\/postgresql-regexp_split_to_table\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.pgtutorial.com\\\/postgresql-string-functions\\\/postgresql-regexp_split_to_table\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.pgtutorial.com\\\/postgresql-string-functions\\\/postgresql-regexp_split_to_table\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.pgtutorial.com\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"PostgreSQL String Functions\",\"item\":\"https:\\\/\\\/www.pgtutorial.com\\\/postgresql-string-functions\\\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"PostgreSQL REGEXP_SPLIT_TO_TABLE Function\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.pgtutorial.com\\\/#website\",\"url\":\"https:\\\/\\\/www.pgtutorial.com\\\/\",\"name\":\"PostgreSQL Tutorial\",\"description\":\"Learn PostgreSQL from Scratch\",\"alternateName\":\"PostgreSQL\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.pgtutorial.com\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"PostgreSQL REGEXP_SPLIT_TO_TABLE Function","description":"Learn how to use the PostgreSQL REGEXP_SPLIT_TO_TABLE function to split a string into multiple rows based on a regular expression pattern.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.pgtutorial.com\/postgresql-string-functions\/postgresql-regexp_split_to_table\/","og_locale":"en_US","og_type":"article","og_title":"PostgreSQL REGEXP_SPLIT_TO_TABLE Function","og_description":"Learn how to use the PostgreSQL REGEXP_SPLIT_TO_TABLE function to split a string into multiple rows based on a regular expression pattern.","og_url":"https:\/\/www.pgtutorial.com\/postgresql-string-functions\/postgresql-regexp_split_to_table\/","og_site_name":"PostgreSQL Tutorial","article_modified_time":"2025-02-17T06:18:49+00:00","twitter_card":"summary_large_image","schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.pgtutorial.com\/postgresql-string-functions\/postgresql-regexp_split_to_table\/","url":"https:\/\/www.pgtutorial.com\/postgresql-string-functions\/postgresql-regexp_split_to_table\/","name":"PostgreSQL REGEXP_SPLIT_TO_TABLE Function","isPartOf":{"@id":"https:\/\/www.pgtutorial.com\/#website"},"datePublished":"2025-02-17T04:50:03+00:00","dateModified":"2025-02-17T06:18:49+00:00","description":"Learn how to use the PostgreSQL REGEXP_SPLIT_TO_TABLE function to split a string into multiple rows based on a regular expression pattern.","breadcrumb":{"@id":"https:\/\/www.pgtutorial.com\/postgresql-string-functions\/postgresql-regexp_split_to_table\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.pgtutorial.com\/postgresql-string-functions\/postgresql-regexp_split_to_table\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.pgtutorial.com\/postgresql-string-functions\/postgresql-regexp_split_to_table\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.pgtutorial.com\/"},{"@type":"ListItem","position":2,"name":"PostgreSQL String Functions","item":"https:\/\/www.pgtutorial.com\/postgresql-string-functions\/"},{"@type":"ListItem","position":3,"name":"PostgreSQL REGEXP_SPLIT_TO_TABLE Function"}]},{"@type":"WebSite","@id":"https:\/\/www.pgtutorial.com\/#website","url":"https:\/\/www.pgtutorial.com\/","name":"PostgreSQL Tutorial","description":"Learn PostgreSQL from Scratch","alternateName":"PostgreSQL","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.pgtutorial.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"}]}},"_links":{"self":[{"href":"https:\/\/www.pgtutorial.com\/wp-json\/wp\/v2\/pages\/2139","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.pgtutorial.com\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.pgtutorial.com\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.pgtutorial.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.pgtutorial.com\/wp-json\/wp\/v2\/comments?post=2139"}],"version-history":[{"count":6,"href":"https:\/\/www.pgtutorial.com\/wp-json\/wp\/v2\/pages\/2139\/revisions"}],"predecessor-version":[{"id":2146,"href":"https:\/\/www.pgtutorial.com\/wp-json\/wp\/v2\/pages\/2139\/revisions\/2146"}],"up":[{"embeddable":true,"href":"https:\/\/www.pgtutorial.com\/wp-json\/wp\/v2\/pages\/1800"}],"wp:attachment":[{"href":"https:\/\/www.pgtutorial.com\/wp-json\/wp\/v2\/media?parent=2139"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}