One way is that:
- you define a cursor for your select using
DECLARE CURSOR[
^]
- loop through the records using
FETCH NEXT[
^]
- in the loop use
CHARINDEX[
^] to find the number of occurences
If you want to return several rows, you can create a
temporary table[
^] and add rows to return into that table and in the end select from that table in order to return the results.
However, if you're only interested in rows having most of certain character, you don't need a procedure for that. You can count the number of occurences by comparing the original length of a string to a length of a string where desired characters are removed. Consider the following:
select SomeColumn,
len(SomeColumn) - len(replace(SomeColumn, 'SomeCharacter', '')) as numberofoccurences
from sometable