{"id":36152,"date":"2024-07-06T11:12:54","date_gmt":"2024-07-06T01:12:54","guid":{"rendered":"https:\/\/database.guide\/?p=36152"},"modified":"2024-07-06T11:12:55","modified_gmt":"2024-07-06T01:12:55","slug":"using-array_replace-in-postgresql","status":"publish","type":"post","link":"https:\/\/database.guide\/using-array_replace-in-postgresql\/","title":{"rendered":"Using ARRAY_REPLACE() in PostgreSQL"},"content":{"rendered":"\n<p class=\"\">In PostgreSQL we can use the <code>array_replace()<\/code> function to replace a specified element (or elements) in an array with another element.<\/p>\n\n\n\n<p class=\"\">The first argument is the array, the second is the element to replace, and the third is the element to replace it with.<\/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 a simple example to demonstrate:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT array_replace(ARRAY&#91;'Cat','Dog','Cat','Horse'], 'Cat', 'Bird');<\/code><\/pre>\n\n\n\n<p class=\"\">Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">{Bird,Dog,Bird,Horse}<\/pre>\n\n\n\n<p class=\"\">Here I replaced <code>Cat<\/code> with <code>Bird<\/code>. There were two occurrences and so both occurrences were replaced.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">No Match<\/h2>\n\n\n\n<p class=\"\">If there are no matching elements nothing is replaced:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT array_replace(ARRAY&#91;'Cat','Dog','Cat','Horse'], 'Bull', 'Bird');<\/code><\/pre>\n\n\n\n<p class=\"\">Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">{Cat,Dog,Cat,Horse}<\/pre>\n\n\n\n<p class=\"\">In this case an identical array was returned.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Wrong Types<\/h2>\n\n\n\n<p class=\"\">Here&#8217;s what happens when the second argument&#8217;s type doesn&#8217;t match the array type:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT array_replace(ARRAY&#91;'Cat','Dog','Cat','Horse'], 1, 'Bird');<\/code><\/pre>\n\n\n\n<p class=\"\">Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">ERROR:  function array_replace(text[], integer, unknown) does not exist<br>LINE 1: SELECT array_replace(ARRAY['Cat','Dog','Cat','Horse'], 1, 'B...<br>               ^<br>HINT:  No function matches the given name and argument types. You might need to add explicit type casts.<\/pre>\n\n\n\n<p class=\"\">Same with the third argument:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT array_replace(ARRAY&#91;'Cat','Dog','Cat','Horse'], 'Bull', 1);<\/code><\/pre>\n\n\n\n<p class=\"\">Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">ERROR:  function array_replace(text[], unknown, integer) does not exist<br>LINE 1: SELECT array_replace(ARRAY['Cat','Dog','Cat','Horse'], 'Bull...<br>               ^<br>HINT:  No function matches the given name and argument types. You might need to add explicit type casts.<\/pre>\n\n\n\n<p class=\"\">It is possible to overcome this error by using single quotes:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT \n    array_replace(ARRAY&#91;'Cat','Dog','Cat','Horse'], '1', 'Bird'),\n    array_replace(ARRAY&#91;'Cat','Dog','Cat','Horse'], 'Bull', '1');<\/code><\/pre>\n\n\n\n<p class=\"\">Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">    array_replace    |    array_replace    <br>---------------------+---------------------<br> {Cat,Dog,Cat,Horse} | {Cat,Dog,Cat,Horse}<\/pre>\n\n\n\n<p class=\"\">Or we could explicitly cast the integers as text:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT \n    array_replace(ARRAY&#91;'Cat','Dog','Cat','Horse'], 1::text, 'Bird'),\n    array_replace(ARRAY&#91;'Cat','Dog','Cat','Horse'], 'Bull', 1::text);<\/code><\/pre>\n\n\n\n<p class=\"\">Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">    array_replace    |    array_replace    <br>---------------------+---------------------<br> {Cat,Dog,Cat,Horse} | {Cat,Dog,Cat,Horse}<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Multi Dimensional Arrays<\/h2>\n\n\n\n<p class=\"\">The <code>array_repace()<\/code> function works with multi dimensional arrays:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT array_replace(ARRAY&#91;ARRAY&#91;'Cat','Dog'],ARRAY&#91;'Cat','Horse']], 'Cat', 'Bird');<\/code><\/pre>\n\n\n\n<p class=\"\">Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">       array_replace       <br>---------------------------<br> {{Bird,Dog},{Bird,Horse}}<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Replacing NULL Values<\/h2>\n\n\n\n<p class=\"\">We can use <code>array_replace()<\/code> to replace NULL values:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT array_replace(ARRAY&#91;null,'Dog',null,'Horse'], null, 'Bird');<\/code><\/pre>\n\n\n\n<p class=\"\">Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">{Bird,Dog,Bird,Horse}<\/pre>\n\n\n\n<p class=\"\">We can also swap it around so that we replace a non-null value with <code>null<\/code>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT array_replace(ARRAY&#91;'Cat','Dog','Cat','Horse'], 'Cat', null);<\/code><\/pre>\n\n\n\n<p class=\"\">Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">{NULL,Dog,NULL,Horse}<\/pre>\n\n\n\n<p class=\"\"><\/p>\n\n\n\n<p class=\"\"><\/p>\n\n\n\n<p class=\"\"><\/p>\n\n\n\n<p class=\"\"><\/p>\n\n\n\n<p class=\"\"><\/p>\n\n\n\n<p class=\"\"><\/p>\n\n\n\n<p class=\"\"><\/p>\n\n\n\n<p class=\"\"><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In PostgreSQL we can use the array_replace() function to replace a specified element (or elements) in an array with another element. The first argument is the array, the second is the element to replace, and the third is the element to replace it with.<\/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,20],"class_list":["post-36152","post","type-post","status-publish","format-standard","hentry","category-postgresql","tag-arrays","tag-functions","tag-what-is"],"_links":{"self":[{"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/posts\/36152","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=36152"}],"version-history":[{"count":5,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/posts\/36152\/revisions"}],"predecessor-version":[{"id":36634,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/posts\/36152\/revisions\/36634"}],"wp:attachment":[{"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/media?parent=36152"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/categories?post=36152"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/tags?post=36152"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}