Received: from localhost (unknown [200.46.204.183]) by mail.postgresql.org (Postfix) with ESMTP id F3876650350 for ; Tue, 9 Dec 2008 14:22:46 -0400 (AST) Received: from mail.postgresql.org ([200.46.204.86]) by localhost (mx1.hub.org [200.46.204.183]) (amavisd-maia, port 10024) with ESMTP id 38991-05 for ; Tue, 9 Dec 2008 14:22:44 -0400 (AST) X-Greylist: delayed 00:07:31.884227 by SQLgrey-1.7.6 Received: from mail-bw0-f20.google.com (mail-bw0-f20.google.com [209.85.218.20]) by mail.postgresql.org (Postfix) with ESMTP id 895C5650338 for ; Tue, 9 Dec 2008 14:22:43 -0400 (AST) Received: by bwz13 with SMTP id 13so139940bwz.19 for ; Tue, 09 Dec 2008 10:22:42 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:received:received:message-id:date:from:to :subject:cc:in-reply-to:mime-version:content-type :content-transfer-encoding:content-disposition:references; bh=wOi6oYlvv4DHDMiPp2xzLntkMYq0TVuMHUPwcYC+4oA=; b=sWijSg1fz5WMHtHE1T1lEsG2OcfhxXMaTuLorhex3ZP2ARPFppciIkJuMkU0DYLDbH OFRVqkgz/MacqNr7tXnagmydSVPDV5ml6yrDTeJ9fKqLkEChjDeVtZVzLw4c+rdjlODX sDuPBKtfI2PIcx/OvJb+RUbjA/aYqNJwctppE= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=message-id:date:from:to:subject:cc:in-reply-to:mime-version :content-type:content-transfer-encoding:content-disposition :references; b=C/0Dp+58H0A9wiiYgaq7S+KsGi3Mc/IOoOPKXXxHmzsMD8riSlFrn32rj0b3ExgezW QnWeFR3CBJDWBjzLP8CkH35h4qlmp/DLO9MTPiyTOSQq6q7/nod0vzm5/STTD/fg0gIf vCBgOLrhydRYGJ0bjlj03a8Uz6CK2C6Ruywjs= Received: by 10.223.106.142 with SMTP id x14mr448123fao.23.1228846509030; Tue, 09 Dec 2008 10:15:09 -0800 (PST) Received: by 10.223.118.195 with HTTP; Tue, 9 Dec 2008 10:15:08 -0800 (PST) Message-ID: Date: Tue, 9 Dec 2008 19:15:08 +0100 From: "Pawel Socha" To: ss_postgres Subject: Re: How to find the position of a characted in a string Cc: pgsql-admin@postgresql.org In-Reply-To: <6c1264780812080234k6c249d9aje780f312c8adff4@mail.gmail.com> MIME-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Content-Disposition: inline References: <6c1264780812072230v6d89cbf4hf618c035d98319b4@mail.gmail.com> <6c1264780812080234k6c249d9aje780f312c8adff4@mail.gmail.com> X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=0 tagged_above=0 required=5 tests=none X-Spam-Level: X-Archive-Number: 200812/70 X-Sequence-Number: 30903 2008/12/8 ss_postgres : > > > On Mon, Dec 8, 2008 at 5:44 PM, Pawel Socha wrote: >> >> 2008/12/8 ss_postgres : >> > 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. Just create function ;) > 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. 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 where x.case is not null; > Thanks for your help. > > -- Serdecznie pozdrawiam Pawel Socha pawel.socha@gmail.com programista/administrator perl -le 's**02).4^&-%2,).^9%4^!./4(%2^3,!#+7!2%^53%2&**y%& -;^[%"`-{ a%%s%%$_%ee'