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

From Pavel Stehule
Subject Re: String function to Find how many times str2 is in str1?
Date
Msg-id 162867790802151023i5a2276bj17f3f666aa4e6827@mail.gmail.com
Whole thread Raw
In response to Re: String function to Find how many times str2 is in str1?  ("Rodrigo E. De León Plicet" <rdeleonp@gmail.com>)
Responses Re: String function to Find how many times str2 is in str1?
List pgsql-sql
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 <rdeleonp@gmail.com> wrote:
> 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.
>
>  ---------------------------(end of broadcast)---------------------------
>  TIP 2: Don't 'kill -9' the postmaster
>

pgsql-sql by date:

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