'Find and Remove Repeated Substrings

I've a column in a SQL Server 2008 table where part of the string was accidentally repeated.

Does anyone have a quick and easy way to remove the trailing duplicated substring?

For example,

alpha\bravo\charlie\delta\charlie\delta

should be

alpha\bravo\charlie\delta


Solution 1:[1]

create function RemoveDups(@S nvarchar(max)) returns nvarchar(max)
as
begin
  declare @R nvarchar(max)
  declare @W nvarchar(max)
  set @R = ''

  while len(@S) > 1
  begin
    -- Get the first word
    set @W = left(@S, charindex('/', @S+'/')-1)

    -- Add word to result if not already added
    if '/'+@R not like '%/'+@W+'/%'
    begin
      set @R = @R + @W + '/'
    end 

    -- Remove first word
    set @S = stuff(@S, 1, charindex('/', @S+'/'), '')
  end

  return left(@R, len(@R)- 1)
end

As requested by Aaron Bertrand. I will however make no claim on what is the fastest to execute.

-- Table to replace in
declare @T table
(
  ID int identity,
  Value nvarchar(max)
)

-- Add some sample data
insert into @T values ('alpha/beta/alpha/gamma/delta/gamma/delta/alpha')
insert into @T values ('delta/beta/alpha/beta/alpha/gamma/delta/gamma/delta/alpha')

-- Update the column
update T
set Value = NewValue
from (
       select T1.ID,
              Value,
              stuff((select '/' + T4.Value
                     from (
                            select T3.X.value('.', 'nvarchar(max)') as Value,
                                   row_number() over(order by T3.X) as rn
                            from T2.X.nodes('/x') as T3(X)
                          ) as T4
                     group by T4.Value
                     order by min(T4.rn)
                     for xml path(''), type).value('.',  'nvarchar(max)'), 1, 1, '') as NewValue
       from @T as T1
         cross apply (select cast('<x>'+replace(T1.Value, '/', '</x><x>')+'</x>' as xml)) as T2(X)
     ) as T

select *
from @T  

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Community