我在SQL Server 2008表中有一个列,其中部分字符串被意外重复。
有没有人能够快速简便地删除尾随的重复子字符串?
例如,
alpha\bravo\charlie\delta\charlie\delta
应该是
alpha\bravo\charlie\delta
答案 0 :(得分:7)
如果您还没有数字表:
SET NOCOUNT ON;
DECLARE @UpperLimit INT;
SET @UpperLimit = 4000;
WITH n(rn) AS
(
SELECT ROW_NUMBER() OVER (ORDER BY [object_id])
FROM sys.all_columns
)
SELECT [Number] = rn - 1
INTO dbo.Numbers FROM n
WHERE rn <= @UpperLimit + 1;
CREATE UNIQUE CLUSTERED INDEX n ON dbo.Numbers([Number]);
现在是一个通用的分割函数,它会将分隔的字符串转换为集合:
CREATE FUNCTION dbo.SplitString
(
@List NVARCHAR(MAX),
@Delim CHAR(1)
)
RETURNS TABLE
AS
RETURN ( SELECT
rn,
vn = ROW_NUMBER() OVER (PARTITION BY [Value] ORDER BY rn),
[Value]
FROM
(
SELECT
rn = ROW_NUMBER() OVER (ORDER BY CHARINDEX(@Delim, @List + @Delim)),
[Value] = LTRIM(RTRIM(SUBSTRING(@List, [Number],
CHARINDEX(@Delim, @List + @Delim, [Number]) - [Number])))
FROM dbo.Numbers
WHERE Number <= LEN(@List)
AND SUBSTRING(@Delim + @List, [Number], 1) = @Delim
) AS x
);
GO
然后是一个将它们重新组合在一起的函数:
CREATE FUNCTION dbo.DedupeString
(
@List NVARCHAR(MAX)
)
RETURNS NVARCHAR(MAX)
AS
BEGIN
RETURN ( SELECT newval = STUFF((
SELECT '\' + x.[Value] FROM dbo.SplitString(@List, '\') AS x
WHERE (x.vn = 1)
ORDER BY x.rn
FOR XML PATH, TYPE).value('.', 'nvarchar(max)'), 1, 1, '')
);
END
GO
样本用法:
SELECT dbo.DedupeString('alpha\bravo\bravo\charlie\delta\bravo\charlie\delta');
结果:
alpha\bravo\charlie\delta
你也可以这样说:
UPDATE dbo.MessedUpTable
SET OopsColumn = dbo.DedupeString(OopsColumn);
@MikaelEriksson可能会以更有效的方式使用XML来消除重复,但这是我在此之前可以提供的。 : - )
答案 1 :(得分:4)
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
根据Aaron Bertrand的要求。但是,我不会对最快执行的内容提出任何要求。
-- 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