{"id":36165,"date":"2024-06-28T11:31:30","date_gmt":"2024-06-28T01:31:30","guid":{"rendered":"https:\/\/database.guide\/?p=36165"},"modified":"2024-06-28T11:31:31","modified_gmt":"2024-06-28T01:31:31","slug":"understanding-string_to_array-in-postgresql","status":"publish","type":"post","link":"https:\/\/database.guide\/understanding-string_to_array-in-postgresql\/","title":{"rendered":"Understanding STRING_TO_ARRAY() in PostgreSQL"},"content":{"rendered":"\n<p class=\"\">In PostgreSQL, <code>string_to_array()<\/code> is a string function that allows us to create an array from a string. It splits the string based on the specified delimiter and returns a <code>text<\/code> array as a result.<\/p>\n\n\n\n<p class=\"\">If we specify a null delimiter, then each character becomes an element in the array. If the delimiter string is empty, then the whole string becomes a single element in the array.<\/p>\n\n\n\n<p class=\"\">We also have the option of turning a specific substring into <code>null<\/code> if required.<\/p>\n\n\n\n<!--more-->\n\n\n\n<h2 class=\"wp-block-heading\">Example<\/h2>\n\n\n\n<p class=\"\">Here&#8217;s an example to demonstrate the function:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT string_to_array('Cat-Dog-Bird', '-');<\/code><\/pre>\n\n\n\n<p class=\"\">Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">{Cat,Dog,Bird}<\/pre>\n\n\n\n<p class=\"\">In that example a hyphen was the separator. Here&#8217;s one where a space is the separator:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT string_to_array('Cat Dog Bird', ' ');<\/code><\/pre>\n\n\n\n<p class=\"\">Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">{Cat,Dog,Bird}<\/pre>\n\n\n\n<p class=\"\">We can also specify multiple characters as the separator:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT string_to_array('Cat, Dog, Bird', ', ');<\/code><\/pre>\n\n\n\n<p class=\"\">Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">{Cat,Dog,Bird}<\/pre>\n\n\n\n<p class=\"\">In this example I specified that a comma followed by a space is the separator.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Passing a null Separator<\/h2>\n\n\n\n<p class=\"\">Specifying <code>null<\/code> for the separator results in each character being a separate element in the array:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT string_to_array('Cat-Dog-Bird', null);<\/code><\/pre>\n\n\n\n<p class=\"\">Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">{C,a,t,-,D,o,g,-,B,i,r,d}<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Passing an Empty String as a Separator<\/h2>\n\n\n\n<p class=\"\">Specifying an empty string as the separator results in the whole string being treated as a single element in the array:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT string_to_array('Cat-Dog-Bird', '');<\/code><\/pre>\n\n\n\n<p class=\"\">Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">{Cat-Dog-Bird}<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Converting a Substring to <code>null<\/code><\/h2>\n\n\n\n<p class=\"\">We can pass an optional third argument to specify that a certain substring is output as <code>null<\/code> in the resulting array:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT string_to_array('Cat-Dog-Bird', '-', 'Dog');<\/code><\/pre>\n\n\n\n<p class=\"\">Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">{Cat,NULL,Bird}<\/pre>\n\n\n\n<p class=\"\">But if the specified value doesn&#8217;t exist in the string, then it has no effect on the resulting array:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT string_to_array('Cat-Dog-Bird', '-', 'Bull');<\/code><\/pre>\n\n\n\n<p class=\"\">Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">{Cat,Dog,Bird}<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Creating a Multi Dimensional Array<\/h2>\n\n\n\n<p class=\"\">We can pass <code>string_to_array()<\/code> to the <a href=\"https:\/\/database.guide\/understanding-the-array-constructor-in-postgresql\/\" data-type=\"post\" data-id=\"35582\"><code>ARRAY<\/code> constructor<\/a> in order to create a multi dimensional array:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT ARRAY&#91;string_to_array('Cat-Dog-Bird', '-')];<\/code><\/pre>\n\n\n\n<p class=\"\">Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">{{Cat,Dog,Bird}}<\/pre>\n\n\n\n<p class=\"\">Here it is with multiple calls to <code>string_to_array()<\/code>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT ARRAY&#91;string_to_array('Cat-Dog-Bird', '-'),string_to_array('Bull, Rat, Ant', ', ')];<\/code><\/pre>\n\n\n\n<p class=\"\">Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">{{Cat,Dog,Bird},{Bull,Rat,Ant}}<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>In PostgreSQL, string_to_array() is a string function that allows us to create an array from a string. It splits the string based on the specified delimiter and returns a text array as a result. If we specify a null delimiter, then each character becomes an element in the array. If the delimiter string is empty, &#8230; <a title=\"Understanding STRING_TO_ARRAY() in PostgreSQL\" class=\"read-more\" href=\"https:\/\/database.guide\/understanding-string_to_array-in-postgresql\/\" aria-label=\"Read more about Understanding STRING_TO_ARRAY() in PostgreSQL\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[40],"tags":[169,93,67,20],"class_list":["post-36165","post","type-post","status-publish","format-standard","hentry","category-postgresql","tag-arrays","tag-functions","tag-string-functions","tag-what-is"],"_links":{"self":[{"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/posts\/36165","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/comments?post=36165"}],"version-history":[{"count":5,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/posts\/36165\/revisions"}],"predecessor-version":[{"id":36636,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/posts\/36165\/revisions\/36636"}],"wp:attachment":[{"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/media?parent=36165"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/categories?post=36165"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/tags?post=36165"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}