Solved

SQL - Count keyword in string

Posted on 2014-10-10
4
118 Views
Last Modified: 2014-10-15
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
Comment
Question by:holemania
  • 2
4 Comments
 
LVL 73

Accepted Solution

by:
sdstuber earned 250 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

by:santanu30in
santanu30in earned 250 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

by:holemania
ID: 40381013
Thank you.  Both solution works equally well.
0
 
LVL 73

Expert Comment

by:sdstuber
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

Open in new window

)
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

In SQL Server, when rows are selected from a table, does it retrieve data in the order in which it is inserted?  Many believe this is the case. Let us try to examine for ourselves with an example. To get started, use the following script, wh…
'Between' is such a common word we rarely think about it but in SQL it has a very specific definition we should be aware of. While most database vendors will have their own unique phrases to describe it (see references at end) the concept in common …
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

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

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

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now