Re: parsing fields - Mailing list pgsql-general

From Joe Conway
Subject Re: parsing fields
Date
Msg-id 3D7F7E08.7060907@joeconway.com
Whole thread Raw
In response to parsing fields  ("Douglas Blood" <dblood@matraex.com>)
List 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




pgsql-general by date:

Previous
From: pginfo
Date:
Subject: SQL syntax analyze tools wanted
Next
From: Jan Wieck
Date:
Subject: Re: transactions