我在 SQL Server 2008 表中有一个列,其中部分字符串被意外重复。
有没有人有一种快速简便的方法来删除尾随的重复子字符串?
例如,
alpha\bravo\charlie\delta\charlie\delta
应该
alpha\bravo\charlie\delta
我在 SQL Server 2008 表中有一个列,其中部分字符串被意外重复。
有没有人有一种快速简便的方法来删除尾随的重复子字符串?
例如,
alpha\bravo\charlie\delta\charlie\delta
应该
alpha\bravo\charlie\delta
如果您还没有数字表:
SET NOCOUNT ON;
DECLARE @UpperLimit int = 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 N'\' + x.[Value] FROM dbo.SplitString(@List, '\') AS x
WHERE (x.vn = 1)
ORDER BY x.rn
FOR XML PATH(''), TYPE).value(N'./text()[1]', N'nvarchar(max)'), 1, 1, N'')
);
END
GO
示例用法:
SELECT dbo.DedupeString(N'alpha\bravo\bravo\charlie\delta\bravo\charlie\delta');
结果:
alpha\bravo\charlie\delta
你也可以这样说:
UPDATE dbo.MessedUpTable
SET OopsColumn = dbo.DedupeString(OopsColumn);
@MikaelEriksson 可能会以一种更有效的方式使用 XML 消除重复项,但在那之前我可以提供。:-)
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