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

From: Rodrigo E(dot) De León Plicet <rdeleonp(at)gmail(dot)com>
To: emilu(at)encs(dot)concordia(dot)ca
Cc: "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 17:46:22
Message-ID: a55915760802150946t7cd4b0a6x7ba31f28c6948bff@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

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)

Good luck.

In response to

Responses

Browse pgsql-sql by date

  From Date Subject
Next Message Dean Gibson (DB Administrator) 2008-02-15 18:18:56 Re: FUNCTIONs and CASTs
Previous Message Emi Lu 2008-02-15 16:09:40 String function to Find how many times str2 is in str1?