Re: String function to Find how many times str2 is in str1?

From: Emi Lu <emilu(at)encs(dot)concordia(dot)ca>
To: Pavel Stehule <pavel(dot)stehule(at)gmail(dot)com>
Cc: "Rodrigo E(dot) De León Plicet" <rdeleonp(at)gmail(dot)com>, PostgreSQL SQL List <pgsql-sql(at)postgresql(dot)org>
Subject: Re: String function to Find how many times str2 is in str1?
Date: 2008-02-15 18:28:14
Message-ID: 47B5D9BE.7050002@encs.concordia.ca
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

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 <rdeleonp(at)gmail(dot)com> wrote:
>> On Fri, Feb 15, 2008 at 11:09 AM, Emi Lu <emilu(at)encs(dot)concordia(dot)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)
>>
If there is not a system func for this, I will do this way.
Thank you for all inputs.
Ly

In response to

Browse pgsql-sql by date

  From Date Subject
Next Message Tom Lane 2008-02-15 22:32:25 Re: FUNCTIONs and CASTs
Previous Message Pavel Stehule 2008-02-15 18:23:12 Re: String function to Find how many times str2 is in str1?