Received: from localhost (unknown [200.46.204.183]) by postgresql.org (Postfix) with ESMTP id 60CBD2E002C for ; Fri, 15 Feb 2008 14:28:35 -0400 (AST) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.183]) (amavisd-maia, port 10024) with ESMTP id 21568-05 for ; Fri, 15 Feb 2008 14:28:32 -0400 (AST) Received: from perseverance.services.encs.concordia.ca (perseverance-96.encs.concordia.ca [132.205.96.94]) by postgresql.org (Postfix) with ESMTP id AFD5B2E002D for ; Fri, 15 Feb 2008 14:28:31 -0400 (AST) Received: from cacao.encs.concordia.ca (emilu@cacao.encs.concordia.ca [132.205.47.225]) by perseverance.services.encs.concordia.ca (envelope-from emilu@encs.concordia.ca) (8.13.7/8.13.7) with ESMTP id m1FISErl013568; Fri, 15 Feb 2008 13:28:15 -0500 Message-ID: <47B5D9BE.7050002@encs.concordia.ca> Date: Fri, 15 Feb 2008 13:28:14 -0500 From: Emi Lu Reply-To: emilu@encs.concordia.ca User-Agent: Thunderbird 2.0.0.6 (X11/20070728) MIME-Version: 1.0 To: Pavel Stehule CC: =?UTF-8?B?IlJvZHJpZ28gRS4gRGUgTGXDs24gUGxpY2V0Ig==?= , PostgreSQL SQL List Subject: Re: String function to Find how many times str2 is in str1? References: <47B5B944.4010203@encs.concordia.ca> <162867790802151023i5a2276bj17f3f666aa4e6827@mail.gmail.com> In-Reply-To: <162867790802151023i5a2276bj17f3f666aa4e6827@mail.gmail.com> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit X-Scanned-By: MIMEDefang 2.58 on perseverance.encs.concordia.ca at 2008/02/15 13:28:15 EST X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200802/133 X-Sequence-Number: 30224 Pavel Stehule wrote: > Hello > > what about > > CREATE OR REPLACE FUNCTION Foobar(text, text) > RETURNS integer AS $$ > SELECT array_upper(string_to_array($1,$2),1) - 1; > $$ LANGUAGE SQL IMMUTABLE; > > On 15/02/2008, Rodrigo E. De León Plicet wrote: >> On Fri, Feb 15, 2008 at 11:09 AM, Emi Lu wrote: >> > Str1 = "test test caa dtest testing EndofString"; >> > Str2 = " "; >> > >> > select funcName(Str1, Str2); >> > >> > return 5 >> >> >> CREATE OR REPLACE FUNCTION >> FOOBAR(TEXT,TEXT) >> RETURNS INT AS $$ >> SELECT(LENGTH($1) - LENGTH(REPLACE($1, $2, ''))) / LENGTH($2) ; >> $$ LANGUAGE SQL IMMUTABLE; >> >> SELECT FOOBAR('test test caa dtest testing EndofString', ' '); >> >> foobar >> -------- >> 5 >> (1 row) >> If there is not a system func for this, I will do this way. Thank you for all inputs. Ly