{"id":843,"date":"2016-02-11T08:00:02","date_gmt":"2016-02-11T15:00:02","guid":{"rendered":"http:\/\/www.sqldbpros.com\/?p=843"},"modified":"2016-02-10T17:27:19","modified_gmt":"2016-02-11T00:27:19","slug":"postgresgreenplum-get-the-last-two-values-in-a-delimited-string","status":"publish","type":"post","link":"http:\/\/sqldbpros.com\/wordpress\/2016\/02\/postgresgreenplum-get-the-last-two-values-in-a-delimited-string\/","title":{"rendered":"Postgres\/Greenplum Get The Last Two Values In A Delimited String"},"content":{"rendered":"<p>Yuck. This works and I'm recording it here in case I need it again but yuck.<\/p>\n<p>Here's the scenario: there is a column in a database with a delimited string. The number of delimited values can vary. SOmetimes there will be one delimited value, sometimes there will be twenty. For this specific case I needed to get the last two values. If we weren't on GreenPlum I would probably go with using the REVERSE function combines with POSITION and SUBSTRING. But alas, REVERSE is not available in GreenPlum (Postgres 8.2). So after much head scratching I came up with the following which will get the last two values and then concatenate them together with a pipe delimiter (don't ask, that's what we needed to display in my use case).<\/p>\n<pre>create table phil_test (columnA character varying)\r\n;\r\ninsert into phil_test(columnA) values ('a');\r\ninsert into phil_test(columnA)  values ('a,b,c,d');\r\ninsert into phil_test(columnA)  values ('a,b,c,d,e');\r\ninsert into phil_test(columnA)  values ('a,b,c,d,e,f');\r\n;\r\nselect columnA\r\nfrom phil_test\r\norder by columnA\r\n;\r\nselect \r\ncolumnA\r\n, coalesce((string_to_array(columnA, ','))[(array_upper((string_to_array(columnA, ',')),1)-1)]||' | ','')\r\n\t||(string_to_array(columnA, ','))[array_upper((string_to_array(columnA, ',')),1)] as last_two\r\nfrom phil_test\r\norder by columnA\r\n<\/pre>\n<p>While working this out I found the following posts pretty dang helpful:<\/p>\n<p><a href=\"http:\/\/stackoverflow.com\/questions\/8584967\/split-comma-separated-column-data-into-additional-columns\" target=\"_blank\">Stack Overflow: Split comma separated column data into additional columns<\/a><\/p>\n<p><a href=\"http:\/\/www.postgresql.org\/docs\/8.2\/static\/arrays.html\" target=\"_blank\">PostgreSQL 8.2: Arrays<\/a><\/p>\n<p><a href=\"http:\/\/www.postgresql.org\/docs\/8.2\/static\/functions-array.html\" target=\"_blank\">PostgreSQL 8.2: Array Functions and Operators<\/a><\/p>\n<p><a href=\"http:\/\/www.postgresql.org\/docs\/8.2\/static\/functions-string.html\" target=\"_blank\">PostgreSQL 8.2: String Functions and Operators<\/a>\u00a0(mostly let me know of the very few string functions available to me)<\/p>\n<p>If you've got a better way to solve this problem let me know! Also give me a heads up if you've had a similar requirement.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Yuck. This works and I'm recording it here in case I need it again but yuck. Here's the scenario: there is a column in a database with a delimited string. The number of delimited values can vary. SOmetimes there will be one delimited value, sometimes there will be twenty. For this specific case I needed [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[147,150],"tags":[180,151,181],"class_list":["post-843","post","type-post","status-publish","format-standard","hentry","category-greenplum","category-postgresql","tag-greenplum","tag-postgres","tag-postgresql"],"_links":{"self":[{"href":"http:\/\/sqldbpros.com\/wordpress\/wp-json\/wp\/v2\/posts\/843","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/sqldbpros.com\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/sqldbpros.com\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/sqldbpros.com\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/sqldbpros.com\/wordpress\/wp-json\/wp\/v2\/comments?post=843"}],"version-history":[{"count":1,"href":"http:\/\/sqldbpros.com\/wordpress\/wp-json\/wp\/v2\/posts\/843\/revisions"}],"predecessor-version":[{"id":844,"href":"http:\/\/sqldbpros.com\/wordpress\/wp-json\/wp\/v2\/posts\/843\/revisions\/844"}],"wp:attachment":[{"href":"http:\/\/sqldbpros.com\/wordpress\/wp-json\/wp\/v2\/media?parent=843"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/sqldbpros.com\/wordpress\/wp-json\/wp\/v2\/categories?post=843"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/sqldbpros.com\/wordpress\/wp-json\/wp\/v2\/tags?post=843"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}