Click here to Skip to main content
Click here to Skip to main content
Alternative Tip

Tagged as

Go to top

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

, 3 Jul 2010
Rate this:
Please Sign up or sign in to vote.
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 | :cool:

License

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

Share

About the Author

Andrew Rissing
Software Developer (Senior)
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 Pinmembersamsagaz5-Mar-14 6:06 
AnswerRe: Too complicated PinmemberAndrew Rissing5-Mar-14 9:47 
AnswerClear job, well done Pinmemberabeethadesilva15-Mar-13 3:45 
GeneralReason for my vote of 5 Thank you for sharing this Pinmemberlinuxjr31-Jul-10 18:43 
GeneralNice. PinadminChris Maunder30-Jun-10 7:55 

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

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

| Advertise | Privacy | Mobile
Web01 | 2.8.140921.1 | Last Updated 3 Jul 2010
Article Copyright 2010 by Andrew Rissing
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid