PostgreSQL string_to_table() Function

The PostgreSQL string_to_table() function splits a specified string with the specified delimiter and returns the result as a set.

This function is similar to the regexp_split_to_table() function.

string_to_table() Syntax

This is the syntax of the PostgreSQL string_to_table() function:

string_to_table(string text, delimiter text) -> set

or

string_to_table(string text, delimiter text, null_string text) -> set

Parameters

string

Required. The string to split.

delimiter

Required. The delimiter.

null_string

Required. A string. Elements in the set that equals it will be replaced with NULL.

Return value

The PostgreSQL string_to_table() function returns a set of rows where rows are all parts of a string string splited with the delimiter delimiter.

If delimiter is NULL, all characters in the string will be on a separate line.

If delimiter is an empty string, the entire string will be the only row in the set.

If null_string not NULL, rows matching it in the result set will be replaced by NULL.

string_to_table() Examples

This example shows how to use the string_to_table() function to split a string into a set with a delimiter:

SELECT string_to_table('ab,cd,ef,gh', ',');
 string_to_table
-----------------
 ab
 cd
 ef
 gh

If delimiter is NULL, all characters in the string will be members of the result set, for example:

SELECT string_to_table('ab,cd,ef,gh', NULL);
 string_to_table
-----------------
 a
 b
 ,
 c
 d
 ,
 e
 f
 ,
 g
 h

Here delimiter is NULL, each character in the string becomes a separate row in the set.

If delimiter is an empty string, the entire string will be the only row in the table. for example:

SELECT string_to_table('ab,cd,ef,gh', '');
 string_to_table
-----------------
 {"ab,cd,ef,gh"}

If null_string is not NULL, rows matching it in the result set will be replaced by NULL, for example:

SELECT string_to_table('ab,cd,ef,gh', ',', 'cd');
 string_to_table
-----------------
 ab

 ef
 gh