[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Number of occurances of a substring within a string

Posted on 2014-08-29
2
Medium Priority
?
161 Views
Last Modified: 2014-08-29
strMyString = "a, b, c, d, e"

There are 4 commas

What is the syntax for returning 4 ?

???
intCommaCount = stringfunction(strMyString )

Thanks
0
Comment
Question by:Dovberman
2 Comments
 
LVL 15

Accepted Solution

by:
Vikas Garg earned 2000 total points
ID: 40292538
Hi,

you need to create a function like this

CREATE FUNCTION dbo.CountOccurancesOfString
(
    @searchString nvarchar(max),
    @searchTerm nvarchar(max)
)
RETURNS INT
AS
BEGIN
    return (LEN(@searchString)-LEN(REPLACE(@searchString,@searchTerm,'')))/LEN(@searchTerm)
END

Open in new window



SELECT  dbo.CountOccurancesOfString('a, b, c, d, e', ',')
0
 

Author Closing Comment

by:Dovberman
ID: 40292591
Just what I needed.

Thanks
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
Suggested Courses

872 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