Quantcast
Channel: Answers for "How To Query A Column For 2 Or More Repeating Characters?"
Viewing all articles
Browse latest Browse all 10

Answer by Dave P 1

$
0
0

I think I misunderstood your question. My previous post assumed you were looking for adjacent characters. This will do what you want: count the instances of @c in @in.

CREATE FUNCTION dbo.fn_strcount            
(            
  @in varchar(8000),            
  @c char             
) RETURNS integer AS             
BEGIN            
    DECLARE @i int,            
        @l int,            
        @out int ;            
    DECLARE @ch char ;            
    SELECT  @l = LEN(@in),            
            @i = 1,            
            @out = 0 ;            
    WHILE ( @i <= @l )             
        BEGIN            
            SET @ch = SUBSTRING(@in, @i, 1) ;            
            IF ( @ch = @c )             
                SET @out = @out + 1 ;            
            SET @i = @i + 1 ;            
        END ;            
    RETURN (@out) ;            
END ;            

Viewing all articles
Browse latest Browse all 10

Trending Articles