Showing posts with label translate. Show all posts
Showing posts with label translate. Show all posts

Monday, June 04, 2007

Tip's Corner: counting how many times a character is in a string

Believe it or not, among the dozens of Oracle's built-in SQL functions, there is none for counting the occurrences of a specific character in a source string.

The lack of this function forced me to come up with a quick and dirty solution:
select
:str as "string",
:chr as "character",
length(:str) - length(translate(:str,chr(0)||:chr,chr(0))) as "count"
from dual;


stringcharactercount
AB^CDE^FGHI^JK^^LMNOPQ^RST^UVWX^YZ^8

Note: this works on the assumption that you are never going to count character zero.

Anyone else with a better/faster idea?

yes you can!

Two great ways to help us out with a minimal effort. Click on the Google Plus +1 button above or...
We appreciate your support!

latest articles