pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
String function   to  Find how many times str2 is in str1?
4+ messages / 3 participants
[nested] [flat]

* String function   to  Find how many times str2 is in str1?
@ 2008-02-15 16:09 Emi Lu <emilu@encs.concordia.ca>
  2008-02-15 17:46 ` Re: String function to Find how many times str2 is in str1? Rodrigo E. De León Plicet <rdeleonp@gmail.com>
  0 siblings, 1 reply; 4+ messages in thread

From: Emi Lu @ 2008-02-15 16:09 UTC (permalink / raw)
  To: pgsql-sql

Good morning,

Is there a string function in PSQL to count how many times one str is in 
another string?

For example,
Str1 = "test   test   caa   dtest   testing   EndofString";
Str2 = "   ";

select funcName(Str1, Str2);

return 5

Because Str1 has 5 Str2.

Thanks !
Ly





^ permalink  raw  reply  [nested|flat] 4+ messages in thread

* Re: String function to Find how many times str2 is in str1?
  2008-02-15 16:09 String function   to  Find how many times str2 is in str1? Emi Lu <emilu@encs.concordia.ca>
@ 2008-02-15 17:46 ` Rodrigo E. De León Plicet <rdeleonp@gmail.com>
  2008-02-15 18:23   ` Re: String function to Find how many times str2 is in str1? Pavel Stehule <pavel.stehule@gmail.com>
  0 siblings, 1 reply; 4+ messages in thread

From: Rodrigo E. De León Plicet @ 2008-02-15 17:46 UTC (permalink / raw)
  To: emilu@encs.concordia.ca; +Cc: 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.



^ permalink  raw  reply  [nested|flat] 4+ messages in thread

* Re: String function to Find how many times str2 is in str1?
  2008-02-15 16:09 String function   to  Find how many times str2 is in str1? Emi Lu <emilu@encs.concordia.ca>
  2008-02-15 17:46 ` Re: String function to Find how many times str2 is in str1? Rodrigo E. De León Plicet <rdeleonp@gmail.com>
@ 2008-02-15 18:23   ` Pavel Stehule <pavel.stehule@gmail.com>
  2008-02-15 18:28     ` Re: String function to Find how many times str2 is in str1? Emi Lu <emilu@encs.concordia.ca>
  0 siblings, 1 reply; 4+ messages in thread

From: Pavel Stehule @ 2008-02-15 18:23 UTC (permalink / raw)
  To: Rodrigo E. De León Plicet <rdeleonp@gmail.com>; +Cc: 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
>


^ permalink  raw  reply  [nested|flat] 4+ messages in thread

* Re: String function to Find how many times str2 is in str1?
  2008-02-15 16:09 String function   to  Find how many times str2 is in str1? Emi Lu <emilu@encs.concordia.ca>
  2008-02-15 17:46 ` Re: String function to Find how many times str2 is in str1? Rodrigo E. De León Plicet <rdeleonp@gmail.com>
  2008-02-15 18:23   ` Re: String function to Find how many times str2 is in str1? Pavel Stehule <pavel.stehule@gmail.com>
@ 2008-02-15 18:28     ` Emi Lu <emilu@encs.concordia.ca>
  0 siblings, 0 replies; 4+ messages in thread

From: Emi Lu @ 2008-02-15 18:28 UTC (permalink / raw)
  To: Pavel Stehule <pavel.stehule@gmail.com>; +Cc: "Rodrigo E. De León Plicet" <rdeleonp@gmail.com>; 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@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)
>>
If there is not a system func for this, I will do this way.
Thank you for all inputs.
Ly



^ permalink  raw  reply  [nested|flat] 4+ messages in thread


end of thread, other threads:[~2008-02-15 18:28 UTC | newest]

Thread overview: 4+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2008-02-15 16:09 String function   to  Find how many times str2 is in str1? Emi Lu <emilu@encs.concordia.ca>
2008-02-15 17:46 ` Re: String function to Find how many times str2 is in str1? Rodrigo E. De León Plicet <rdeleonp@gmail.com>
2008-02-15 18:23   ` Re: String function to Find how many times str2 is in str1? Pavel Stehule <pavel.stehule@gmail.com>
2008-02-15 18:28     ` Re: String function to Find how many times str2 is in str1? Emi Lu <emilu@encs.concordia.ca>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox