MS SQL Server : How to remove duplicate text in a csv String in table
Here is the function which will return string after removing duplicate text from a csv cell value.
create function dbo.RemoveDuplicateTextInCSVString(@SOURCE_STR varchar(50))
returns varchar(50)
as
begin
declare @TEMP_STR varchar(50)
declare @WORD_STR varchar(50)
set @TEMP_STR = ','
while len(@SOURCE_STR) > 0
begin
print '--------------------'
set @WORD_STR = left(@SOURCE_STR, charindex(',', @SOURCE_STR+',')-1)+','
print '@WORD_STR :'+ @WORD_STR
if charindex(','+@WORD_STR, @TEMP_STR) = 0
begin
set @TEMP_STR = @TEMP_STR + @WORD_STR
print '@TEMP_STR : '+@TEMP_STR
end
else
print 'found duplicate, remove this..'
set @SOURCE_STR = stuff(@SOURCE_STR, 1, charindex(',', @SOURCE_STR+','), '')
print '@SOURCE_STR '+@SOURCE_STR
end
return @SOURCE_STR
end
create function dbo.RemoveDuplicateTextInCSVString(@SOURCE_STR varchar(50))
returns varchar(50)
as
begin
declare @TEMP_STR varchar(50)
declare @WORD_STR varchar(50)
set @TEMP_STR = ','
while len(@SOURCE_STR) > 0
begin
print '--------------------'
set @WORD_STR = left(@SOURCE_STR, charindex(',', @SOURCE_STR+',')-1)+','
print '@WORD_STR :'+ @WORD_STR
if charindex(','+@WORD_STR, @TEMP_STR) = 0
begin
set @TEMP_STR = @TEMP_STR + @WORD_STR
print '@TEMP_STR : '+@TEMP_STR
end
else
print 'found duplicate, remove this..'
set @SOURCE_STR = stuff(@SOURCE_STR, 1, charindex(',', @SOURCE_STR+','), '')
print '@SOURCE_STR '+@SOURCE_STR
end
return @SOURCE_STR
end
Comments
Post a Comment