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