SQL - Count keyword in string

Trying to google this, but can't find it anywhere.  In my string, I have a set of symbols and I would like to count how many times it repeats.

Example the field is description.  The description field would have the following:


If the # shows once, I want it to count once.  If it shows twice, it'll count twice.  It's always from left to right with the special symbol.  How could I count if there's 0 # for the first line, 1 count for the 2nd line etc?  The last example, if it's not consecutive, I don't want to count the rest of the repeating "Keyword" or symbol.  Is this even possible?
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

I'm assuming the output for the sample data above would be


Is that correct?

if so, try this...


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial

You can do  like below

DECLARE @sString VARCHAR(1000)
SET @sString = '##Testing###'

SELECT LEN(@sString) - LEN(REPLACE(@sString, '#', ''))

Or you can do like below

SELECT (LEN(ColumnName) - LEN(REPLACE(BILLNO, '#', ''))) AS sCount FROM TableName
holemaniaAuthor Commented:
Thank you.  Both solution works equally well.
Actually they don't both work equally well.

The second answer doesn't work.

 It returns the total number of # in the string, not the length of the first consecutive set

select patindex('%[^#]%',str)-1 sdstuber,
       LEN(str) - LEN(REPLACE(str, '#', '')) santanu30in 
(select '##Test String###' str
union all
select '#a#b#c#d#e' str
) x
sdstuber    santanu30in
----------- -----------
2           5
1           5

(2 row(s) affected

Open in new window

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Query Syntax

From novice to tech pro — start learning today.