Click here to Skip to main content
15,881,139 members
Articles / Programming Languages / SQL

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 16.9K   4  
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...

Alternatives

Members may post updates or alternatives to this current article in order to show different approaches or add new features.

Please Sign up or sign in to vote.
15 Aug 2011phillipvoyle
CREATE FUNCTION [dbo].[com_CountString](@Input nVarChar(max), @SearchString nVarChar(1000))RETURNS INTBEGIN if @Input is null or @SearchString is null return 0 DECLARE @InputLength INT, @SearchLength INT SELECT...
Please Sign up or sign in to vote.
25 Jun 2010Chris Maunder 2 alternatives  
I had a need to count the number of times a certain string appeared within a column in a SQL table. I came up with this simple function that may be of use to others

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