Re: parsing fields

From: Joe Conway <mail(at)joeconway(dot)com>
To: Douglas Blood <dblood(at)matraex(dot)com>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: parsing fields
Date: 2002-09-11 17:31:52
Message-ID: 3D7F7E08.7060907@joeconway.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Douglas Blood wrote:
> Is there a way in postgres to count how many instances of a character there
> are in a varchar field?
> I know how I can see if there is any instance of the character using
> strpos() but that will only find if there is one and i would like the total
> count of them.
> Any help would be appriciated.

I can't think of a direct way, but ISTM it would be pretty straightforward to
create a user defined function to do this.

Things to think about:
- does it need to work in a multibyte character database?
- are you always counting occurrences of just one character, or do you also
need to count occurrences of multi-character substrings?
- is your need performance sensitive?
- do you need position of each occurrence as well as total count?
- which PL languages are you the most comfortable with?

Answers to these will influence the function design and might suggest one PL
over the others.

HTH,

Joe

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Jan Wieck 2002-09-11 18:58:04 Re: transactions
Previous Message Roland Roberts 2002-09-11 16:28:54 Re: plpgsql trigger parse error