{"id":36110,"date":"2024-06-11T10:55:25","date_gmt":"2024-06-11T00:55:25","guid":{"rendered":"https:\/\/database.guide\/?p=36110"},"modified":"2024-06-11T10:55:26","modified_gmt":"2024-06-11T00:55:26","slug":"how-to-make-a-zero-based-array-in-postgresql","status":"publish","type":"post","link":"https:\/\/database.guide\/how-to-make-a-zero-based-array-in-postgresql\/","title":{"rendered":"How to Make a Zero-Based Array in PostgreSQL"},"content":{"rendered":"\n<p class=\"\">By default, PostgreSQL arrays are one-based. That means that the numbering starts at subscript 1 and increments with each element in the array. However, we can change this so that the array starts at a different subscript.<\/p>\n\n\n\n<!--more-->\n\n\n\n<h2 class=\"wp-block-heading\">Example<\/h2>\n\n\n\n<p class=\"\">We can use subscripted assignment to create a zero-based array:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT ('&#91;0:2]={ \"Cat\", \"Dog\", \"Horse\" }'::text&#91;]);<\/code><\/pre>\n\n\n\n<p class=\"\">Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">[0:2]={Cat,Dog,Horse}<\/pre>\n\n\n\n<p class=\"\">The bit at the front that goes <code>[0:2]=<\/code> is subscripted assignment that specifies the array&#8217;s range. In this case I specified that it goes from <code>0<\/code> to <code>2<\/code>, which means that it&#8217;s a zero-based array.<\/p>\n\n\n\n<p class=\"\">Now when we want to reference an element, we need to use the subscript that adheres to the array&#8217;s zero-based range:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT ('&#91;0:2]={ \"Cat\", \"Dog\", \"Horse\" }'::text&#91;])&#91;1];<\/code><\/pre>\n\n\n\n<p class=\"\">Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">Dog<\/pre>\n\n\n\n<p class=\"\">In this case I appended <code>[1]<\/code> to the <code>SELECT<\/code> statement, which means that I wanted to return the element at subscript <code>1<\/code>. Given this is a zero-based array, that subscript represents the second element in the array.<\/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>By default, PostgreSQL arrays are one-based. That means that the numbering starts at subscript 1 and increments with each element in the array. However, we can change this so that the array starts at a different subscript.<\/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,10],"class_list":["post-36110","post","type-post","status-publish","format-standard","hentry","category-postgresql","tag-arrays","tag-how-to"],"_links":{"self":[{"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/posts\/36110","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=36110"}],"version-history":[{"count":3,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/posts\/36110\/revisions"}],"predecessor-version":[{"id":36113,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/posts\/36110\/revisions\/36113"}],"wp:attachment":[{"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/media?parent=36110"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/categories?post=36110"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/tags?post=36110"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}