Search the Blog

Monday, April 25, 2022

SQL SERVER FUNCTION TO REMOVE CONSUCATIVE DUPLICATE CHARACTERS

 


CREATE FUNCTION REMOVE_DUPLICATE_CHAR_FROM_STRING(@val NVARCHAR(500))

RETURNS  NVARCHAR(500) 

as

begin

    DECLARE @result nVARCHAR(50);

set @result=''

    DECLARE @prev CHAR(1)

set @prev=''

    DECLARE @c CHAR(1) 

set @c=''

    DECLARE @i TINYINT 

set @i=1

    DECLARE @len TINYINT;


    SET @len = LEN(@val);

    WHILE(@i <= @len) 

begin

        SET @c = SUBSTRING(@val, @i, 1);

        IF @c != @prev 

begin

            SET @result = CONCAT(@result, @c);

            SET @prev = @c;

end

        else

begin

        SET @i = @i + 1;

end

    END 

    return  @result;

end


No comments:

Post a Comment

Translate