PostgreSQL – STRING_TO_ARRAY Function

The string_to_array function is used to split a string into an array of substrings based on a specified delimiter. It takes a string as input and returns an array of text elements as output.

Below is the syntax for the string_to_array function:

string_to_array(input_string, delimiter)

where input_string is the string that you want to split into an array.

and delimiter is the delimiter that defines where the string should be split. It can be a single character or a substring.

Here’s an example of using the string_to_array function:

Consider a string with names separated by commas:

SELECT string_to_array('John,Doe,Alice', ',')

The result of this query would be an array of text elements:Copy code

{John, Doe, Alice}

You can use different delimiters as needed. For example, if your string uses a space as a delimiter:

SELECT string_to_array('John Doe Alice', ' ')

The result would be:

{John, Doe, Alice}

You can use the string_to_array function to parse input data where values are separated by a specific character or substring and then work with the resulting array in your PostgreSQL queries.

Similar Posts