Click here to Skip to main content
15,891,708 members
Articles / Programming Languages / SQL
Alternative
Tip/Trick

Counting the number of occurrences of one string inside another in SQL

Rate me:
Please Sign up or sign in to vote.
5.00/5 (3 votes)
3 Jul 2010CPOL 17K   4   5
Using the pattern that Microsoft SQL Server Management Studio creates, it would be something along the lines of:IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[com_CountString]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))DROP FUNCTION...
Using the pattern that Microsoft SQL Server Management Studio creates, it would be something along the lines of:

IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[com_CountString]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
DROP FUNCTION [dbo].[com_CountString]
GO
/*====================================================================================
  Counts the number of times @SearchString appears in @Input.
  ====================================================================================*/
CREATE FUNCTION [dbo].[com_CountString](@Input nVarChar(max), @SearchString nVarChar(1000))RETURNS INT
BEGIN
    DECLARE
      @Count INT,
      @Index INT,
      @InputLength INT,
      @SearchLength INT,
      @SampleString INT
 
    IF ((@Input IS NULL) OR (@SearchString IS NULL))
        RETURN 0
 
    SELECT
      @Count = 0,
      @Index = 1,
      @InputLength = LEN(@Input),
      @SearchLength = LEN(@SearchString)
 
    IF ((@InputLength = 0) OR (@SearchLength = 0) OR (@SearchLength > @InputLength))
        RETURN 0
 
    WHILE @Index <= @InputLength - @SearchLength + 1
    BEGIN
        IF SUBSTRING(@Input, @Index, @SearchLength) = @SearchString
        BEGIN
            SELECT
              @Count = @Count + 1,
              @Index = @Index + @SearchLength
        END
        ELSE
        BEGIN
            SET @Index = @Index + 1
        END
    END
 
    RETURN @Count
    END
GO

I went ahead and updated the set statements as well. But using the pattern for dropping would get you out of updating it when that signature changes at all. It keeps it a bit more flexible and less tied to the exact implementation. :cool:

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect
United States United States
Since I've begun my profession as a software developer, I've learned one important fact - change is inevitable. Requirements change, code changes, and life changes.

So..If you're not moving forward, you're moving backwards.

Comments and Discussions

 
QuestionToo complicated Pin
samsagaz5-Mar-14 6:06
samsagaz5-Mar-14 6:06 
AnswerRe: Too complicated Pin
Andrew Rissing5-Mar-14 9:47
Andrew Rissing5-Mar-14 9:47 
AnswerClear job, well done Pin
abeethadesilva15-Mar-13 3:45
abeethadesilva15-Mar-13 3:45 
GeneralReason for my vote of 5 Thank you for sharing this Pin
linuxjr31-Jul-10 18:43
professionallinuxjr31-Jul-10 18:43 
GeneralNice. Pin
Chris Maunder30-Jun-10 7:55
cofounderChris Maunder30-Jun-10 7:55 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.