Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1kVzXo-00058O-B9 for pgsql-general@arkaria.postgresql.org; Fri, 23 Oct 2020 16:03:56 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1kVzXn-00035W-9f for pgsql-general@arkaria.postgresql.org; Fri, 23 Oct 2020 16:03:55 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1kVzXn-00035O-2d for pgsql-general@lists.postgresql.org; Fri, 23 Oct 2020 16:03:55 +0000 Received: from smtp-zimbra.meteo.fr ([137.129.16.22] helo=celebrian2.meteo.fr) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1kVzXj-0007G9-39 for pgsql-general@lists.postgresql.org; Fri, 23 Oct 2020 16:03:53 +0000 Received: from localhost (localhost [127.0.0.1]) by celebrian2.meteo.fr (Postfix) with ESMTP id 65BBD484FE for ; Fri, 23 Oct 2020 18:03:48 +0200 (CEST) Received: from celebrian2.meteo.fr ([127.0.0.1]) by localhost (celebrian2.meteo.fr [127.0.0.1]) (amavisd-new, port 10032) with ESMTP id qHBoYudDVdoD for ; Fri, 23 Oct 2020 18:03:48 +0200 (CEST) Received: from localhost (localhost [127.0.0.1]) by celebrian2.meteo.fr (Postfix) with ESMTP id 4BE1F48500 for ; Fri, 23 Oct 2020 18:03:48 +0200 (CEST) X-Virus-Scanned: amavisd-new at celebrian2.meteo.fr Received: from celebrian2.meteo.fr ([127.0.0.1]) by localhost (celebrian2.meteo.fr [127.0.0.1]) (amavisd-new, port 10026) with ESMTP id K3fV8AnisBC7 for ; Fri, 23 Oct 2020 18:03:48 +0200 (CEST) Received: from zumba5.meteo.fr (zumba5 [172.31.9.201]) by celebrian2.meteo.fr (Postfix) with ESMTP id 380AD484FE for ; Fri, 23 Oct 2020 18:03:48 +0200 (CEST) Date: Fri, 23 Oct 2020 18:03:47 +0200 (CEST) From: PALAYRET Jacques To: pgsql-general@lists.postgresql.org Message-ID: <689396068.21102582.1603469027896.JavaMail.zimbra@meteo.fr> In-Reply-To: References: Subject: Re: split_part for the last element MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable X-Originating-IP: [172.31.9.205] X-Mailer: Zimbra 8.7.11_GA_3822 (ZimbraWebClient - FF82 (Win)/8.7.11_GA_3822) Thread-Topic: split_part for the last element Thread-Index: mXTr049ilzSsvRXq6+WyuTSKx/HAbg== List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk Hello, reverse(split_part(reverse('foo bar baz'), ' ', 1)) -> 'baz' Regards ----- Mail original ----- De: "Nikhil Benesch" =C3=80: pgsql-general@lists.postgresql.org Envoy=C3=A9: Vendredi 23 Octobre 2020 17:47:16 Objet: split_part for the last element Hi, Suppose I need to split a string on a delimiter and select one of the resulting components. If I want a specific component counting from the start, that's easy: split_part('foo bar baz', ' ', 1) -> 'foo' But if I want the last component, I have several less-than-ideal options: 1. (string_to_array('foo bar baz', ' '))[cardinality(string_to_array('foo bar baz', ' ')) - 1] 2. reverse(split_part(reverse('foo bar baz'), ' ', 1)) 3. (regexp_match('foo baz bar', '\S*$'))[1] Option 1 is probably the most understandable, especially if you are willing to introduce a temporary parts array: select parts[cardinality(parts) - 1] from string_to_array('foo bar baz', ' ') parts But if the strings are long, this needlessly builds an array just to throw it away. Option 2 has similar efficiency problems and is just kind of silly. Option 3 is probably the best, but it's still a good bit more complicated than a simple split_part invocation. Is there another option I'm missing? Would there be interest in extending split part so that negative indices counted from the end, as in: split_part('foo bar baz', ' ', -1) -> 'baz' Or adding a split_part_end function in which positive indices counted from the end: split_part_end('foo bar baz', ' ', 1) -> 'baz' I'd be happy to prepare a patch if so. Cheers, Nikhil