pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: PALAYRET Jacques <jacques.palayret@meteo.fr>
To: pgsql-general@lists.postgresql.org
Subject: Re: split_part for the last element
Date: Fri, 23 Oct 2020 18:03:47 +0200 (CEST)
Message-ID: <689396068.21102582.1603469027896.JavaMail.zimbra@meteo.fr> (raw)
In-Reply-To: <CAPWqQZR+-5pAZNSSrnmYczRaX-huemc=oO8URvDZvUA-M=MOBA@mail.gmail.com>
References: <CAPWqQZR+-5pAZNSSrnmYczRaX-huemc=oO8URvDZvUA-M=MOBA@mail.gmail.com>

Hello,

  reverse(split_part(reverse('foo bar baz'), ' ', 1))   -> 'baz'

Regards

----- Mail original -----
De: "Nikhil Benesch" <nikhil.benesch@gmail.com>
À: pgsql-general@lists.postgresql.org
Envoyé: 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





view thread (7+ messages)  latest in thread

Message-ID: <689396068.21102582.1603469027896.JavaMail.zimbra@meteo.fr>
Permalink:  ../689396068.21102582.1603469027896.JavaMail.zimbra@meteo.fr/
Also on:    postgresql.org/message-id/689396068.21102582.1603469027896.JavaMail.zimbra@meteo.fr

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-general@postgresql.org
  Cc: jacques.palayret@meteo.fr
  Subject: Re: split_part for the last element
  In-Reply-To: <689396068.21102582.1603469027896.JavaMail.zimbra@meteo.fr>
  References: <CAPWqQZR+-5pAZNSSrnmYczRaX-huemc=oO8URvDZvUA-M=MOBA@mail.gmail.com> <689396068.21102582.1603469027896.JavaMail.zimbra@meteo.fr>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox