Re: String function to Find how many times str2 is in str1? - Mailing list pgsql-sql

From Rodrigo E. De León Plicet
Subject Re: String function to Find how many times str2 is in str1?
Date
Msg-id a55915760802150946t7cd4b0a6x7ba31f28c6948bff@mail.gmail.com
Whole thread Raw
In response to String function to Find how many times str2 is in str1?  (Emi Lu <emilu@encs.concordia.ca>)
Responses Re: String function to Find how many times str2 is in str1?  ("Pavel Stehule" <pavel.stehule@gmail.com>)
List pgsql-sql
On Fri, Feb 15, 2008 at 11:09 AM, Emi Lu <emilu@encs.concordia.ca> 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)

Good luck.


pgsql-sql by date:

Previous
From: Emi Lu
Date:
Subject: String function to Find how many times str2 is in str1?
Next
From: "Dean Gibson (DB Administrator)"
Date:
Subject: Re: FUNCTIONs and CASTs