top of page

Strign repeated in a row

/*How to count a string that number of times has repeated in a table rows.

Here i just passes the values by dynamically. please lets have look as down*/

USE [Your_Database]

GO

/****** Object: StoredProcedure [dbo].[Count_String_Repeated] Script Date: 11/25/2016 9:36:53 AM ******/

SET ANSI_NULLS ON

GO

SET QUOTED_IDENTIFIER ON

GO

Creat procedure [dbo].[Count_String_Repeated]

(

@tblName as varchar(100),

@col_name as varchar(100),

@FindSubString as varchar(100)

)

as

begin

--DECLARE @LongSentence VARCHAR(MAX)

DECLARE @search_string VARCHAR(100)

declare @tbl as VARCHAR(max)

--SET @LongSentence = 'My Super Long String With Long Words'

SET @search_string = @FindSubString

set @tbl='SELECT '+ @col_name+',(LEN('+@col_name+') - LEN(REPLACE('+@col_name+','''+@search_string+''', ''''))) CntReplacedChars,

(LEN('+@col_name+') -LEN(REPLACE('+@col_name+','''+@search_string+''', '''')))/LEN('''+@search_string+''') CntOccuranceChars from '+ @tblName

--set @tbl='

--SELECT ' + @col_name +',(LEN('+ @col_name +') - LEN(REPLACE('+ @col_name +','+ @search_string +',''))) as CntReplacedChars,

--(LEN('+ @col_name +') - LEN(REPLACE('+ @col_name +','+ @search_string +''+')))/LEN('+ @search_string +','') as CntOccuranceChars from '+ @tblName

exec (@tbl)

--select @tbl

end


RECENT POST
bottom of page