Code Snippet
- CREATE FUNCTION[dbo].[FUN_Remove_Duplicate_Char](@Input[varchar](max))
- RETURNS[varchar](max)
- AS
- BEGIN
- DECLARE @Count int;
- DECLARE @Return_String[varchar](max);
- DECLARE @Char_Index int;
- DECLARE @Temp_Str[varchar](max);
- SET @Count = 0;
- SET @Return_String = @Input;
- WHILE @Count <= 255
- BEGIN
- SET @Char_Index = CHARINDEX(CHAR(@Count), @Return_String, 0);
- IF(@Char_Index > 0)
- BEGIN
- SET @Temp_Str = SUBSTRING(@Return_String, @Char_Index + 1, LEN(@Return_String));
- SET @Temp_Str = REPLACE(@Temp_Str, CHAR(@Count), '');
- SET @Return_String = SUBSTRING(@Return_String, 1, @Char_Index) + @Temp_Str;
- END
- SET @Count = @Count + 1;
- END
- RETURN @Return_String;
- END
Example
- SELECT dbo.FUN_Remove_Duplicate_Char('Hello How Are You') AS String_;
Output:
Helo wArYu
No comments:
Post a Comment