I have a string in my stored proc like \',,,sam,,bob,\'
or \',,,\'
from the above string I have to delete multiple commas from it, it must look like
<
I would suggest a UDF to do this. Since the UDF I am about to suggest doesn't touch any tables, the performance should be pretty good.
CREATE Function [dbo].[CleanDuplicates](@Data VarChar(8000), @DuplicateChar VarChar(1))
Returns VarChar(8000)
WITH SCHEMABINDING
AS
Begin
Set @Data = @DuplicateChar + @Data
While PATINDEX('%' + @DuplicateChar + @DuplicateChar + '%',@Data) > 0
Set @Data = REPLACE(@Data, @DuplicateChar + @DuplicateChar,@DuplicateChar)
Return Right(@Data, Len(@Data)-1)
End
You can test the function like this:
Select dbo.CleanDuplicates(',,,', ',')
Select dbo.CleanDuplicates(',,,sam,,bob,', ',')