Solved

# SQL - Count keyword in string

Posted on 2014-10-10
Medium Priority
136 Views
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:

Testing
#Testing
##Testing
###Testing
##Testing###

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?
0
Question by:holemania
• 2

LVL 74

Accepted Solution

sdstuber earned 1000 total points
ID: 40373959
I'm assuming the output for the sample data above would be

0
1
2
3
2

Is that correct?

if so, try this...

patindex('%[^#]%',yourstring)-1
0

LVL 3

Assisted Solution

santanu30in earned 1000 total points
ID: 40375791
hi

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
0

Author Closing Comment

ID: 40381013
Thank you.  Both solution works equally well.
0

LVL 74

Expert Comment

ID: 40382114
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
from
(select '##Test String###' str
union all
select '#a#b#c#d#e' str
) x
sdstuber    santanu30in
----------- -----------
2           5
1           5

(2 row(s) affected
``````
)
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
MySQLTuner is a script written in Perl that allows you to review a MySQL installation quickly and make adjustments to increase performance and stability. The current configuration variables and status data is retrieved and presented in a brief forma…
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
Stellar Phoenix SQL Database Repair software easily fixes the suspect mode issue of SQL Server database. It is a simple process to bring the database from suspect mode to normal mode. Check out the video and fix the SQL database suspect mode problem.
###### Suggested Courses
Course of the Month8 days, 15 hours left to enroll

#### 621 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.