Click here to Skip to main content
11,631,884 members (69,102 online)
Click here to Skip to main content

Tagged as

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

, 3 Jul 2010 CPOL 5.7K 4
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.

You may also be interested in...

Comments and Discussions

 
QuestionToo complicated Pin
samsagaz5-Mar-14 6:06
membersamsagaz5-Mar-14 6:06 
AnswerRe: Too complicated Pin
Andrew Rissing5-Mar-14 9:47
memberAndrew Rissing5-Mar-14 9:47 
AnswerClear job, well done Pin
abeethadesilva15-Mar-13 3:45
memberabeethadesilva15-Mar-13 3:45 
GeneralReason for my vote of 5 Thank you for sharing this Pin
linuxjr31-Jul-10 18:43
memberlinuxjr31-Jul-10 18:43 
GeneralNice. Pin
Chris Maunder30-Jun-10 7:55
adminChris 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 | Terms of Use | Mobile
Web03 | 2.8.150728.1 | Last Updated 3 Jul 2010
Article Copyright 2010 by Andrew Rissing
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid