From: ss_postgres <ss.postgres@gmail.com>
To: Pawel Socha <pawel.socha@gmail.com>
Cc: pgsql-admin@postgresql.org
Subject: Re: How to find the position of a characted in a string
Date: Mon, 8 Dec 2008 21:04:04 +1030
Message-ID: <6c1264780812080234k6c249d9aje780f312c8adff4@mail.gmail.com> (raw)
In-Reply-To: <cc4f12900812072314r161e191vc9d2d0364f187c89@mail.gmail.com>
References: <6c1264780812072230v6d89cbf4hf618c035d98319b4@mail.gmail.com>
<cc4f12900812072314r161e191vc9d2d0364f187c89@mail.gmail.com>
On Mon, Dec 8, 2008 at 5:44 PM, Pawel Socha <pawel.socha@gmail.com> wrote:
> 2008/12/8 ss_postgres <ss.postgres@gmail.com>:
> > Hi,
> >
> > I am trying to find the position of a character in a string which appears
> > multiple times.
> >
> > The POSITION function returns the position of the character when it first
> > occurs.
> >
> > For eg:
> >
> > select position ( ' ' in 'Hi How are you');
> >
> > This returns the result as "3".
> >
> >
> > But i need to result to be
> > 3
> > 7
> > 11
> >
> >
> > Can anyone please let me know what would be the best way to do this.
> >
> > Thanks
> >
>
> My first things:
>
> merlin=> select distinct(x.case) from (select (case when substring('Hi
> How are you' from r for 1) = ' ' then r else null end) from
> generate_series(0, length('Hi How are you')) as r) as x;
> case
> ------
> 3
> 7
> 11
>
>
> --
> Pawel Socha
>
Hi Pawal,
Thanks a lot.
Suppose i need to do the same thing on a column in a table, can you please
let me know how to write the query.
Also how to eliminate the null result at the end? For eg: Running the above
query gives 4 rows, the last row with null value.
Thanks for your help.
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 using the --to and --cc options:
reply via email
To: pgsql-admin@postgresql.org
Cc: ss.postgres@gmail.com, pawel.socha@gmail.com
Subject: Re: How to find the position of a characted in a string
In-Reply-To: <6c1264780812080234k6c249d9aje780f312c8adff4@mail.gmail.com>
* 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