Find and Remove Repeated Substrings

前端 未结 2 734
情歌与酒
情歌与酒 2020-12-21 02:15

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

2条回答
  •  甜味超标
    2020-12-21 02:52

    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(''+replace(T1.Value, '/', '')+'' as xml)) as T2(X)
         ) as T
    
    select *
    from @T  
    

提交回复
热议问题