{"id":2652,"date":"2015-09-21T02:12:30","date_gmt":"2015-09-21T02:12:30","guid":{"rendered":"http:\/\/www.garysieling.com\/blog\/?p=2652"},"modified":"2015-09-21T02:12:30","modified_gmt":"2015-09-21T02:12:30","slug":"postgres-array-concatenation-example","status":"publish","type":"post","link":"https:\/\/www.garysieling.com\/blog\/postgres-array-concatenation-example\/","title":{"rendered":"Postgres array concatenation example"},"content":{"rendered":"<p>Array concatenation in Postgres is really simple, although the operator is non-obvious: ||<\/p>\n<p>This works for several syntactically similar combination of scalars and arrays, as seen here:<\/p>\n<pre lang=\"sql\">\nselect ARRAY['b', 'c', 'd'] || ARRAY['e'];\nselect ARRAY['b', 'c', 'd'] || '{e}';\nselect '{a}' || ARRAY['b', 'c', 'd'];\nselect ARRAY['a'] || ARRAY['b', 'c', 'd'];\nselect array_cat('{a}'::text[], '{b,c,d}'::text[])\nselect 1 || ARRAY[2,3,4]\n<\/pre>\n<p>If you try scalar + array combinations with strings, you&#8217;ll get an error like so:<\/p>\n<pre>\nERROR: array value must start with \"{\" or dimension information Position: 8\n<\/pre>\n<p>This is because Postgres doesn&#8217;t know whether or not to coerce a string you&#8217;ve provided into it&#8217;s array form. The only workaround I know for this is to make a single-element array from the value.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Array concatenation in Postgres is really simple, although the operator is non-obvious: || This works for several syntactically similar combination of scalars and arrays, as seen here: select ARRAY[&#8216;b&#8217;, &#8216;c&#8217;, &#8216;d&#8217;] || ARRAY[&#8216;e&#8217;]; select ARRAY[&#8216;b&#8217;, &#8216;c&#8217;, &#8216;d&#8217;] || &#8216;{e}&#8217;; select &#8216;{a}&#8217; || ARRAY[&#8216;b&#8217;, &#8216;c&#8217;, &#8216;d&#8217;]; select ARRAY[&#8216;a&#8217;] || ARRAY[&#8216;b&#8217;, &#8216;c&#8217;, &#8216;d&#8217;]; select array_cat(&#8216;{a}&#8217;::text[], &#8216;{b,c,d}&#8217;::text[]) select &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.garysieling.com\/blog\/postgres-array-concatenation-example\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Postgres array concatenation example&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[4],"tags":[160,437,523],"aioseo_notices":[],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/posts\/2652"}],"collection":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/comments?post=2652"}],"version-history":[{"count":0,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/posts\/2652\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/media?parent=2652"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/categories?post=2652"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/tags?post=2652"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}