我的 SQL 表中有一列,其中一些值被分隔为 bij "
。
此字符串中的某些值是重复的,我想将其删除。这是我的数据示例:
---------------
| qw"qw"er"er |
---------------
| q"w"w"q |
---------------
| f"k"s"g |
---------------
现在结果应该替换任何重复项:
---------------
| qw"er |
---------------
| q"w" |
---------------
| f"k"s"g |
---------------
所以首先我想拆分字符串,然后删除重复项。谁能帮我解决这个问题?
最佳答案
具有解析功能的选项 1
Declare @YourTable table (ID int,YourCol varchar(50))
Insert Into @YourTable values
(1,'qw"qw"er"er'),
(2,'q"w"w"q'),
(3,'f"k"s"g')
Select A.ID
,A.YourCol
,DeDuped = Stuff((Select '"' + RetVal
From (Select RetSeq=Min(RetSeq),RetVal
From [dbo].[udf-Str-Parse](A.YourCol,'"')
Group By RetVal) P
Order by 1 For XML Path('')),1,1,'')
From @YourTable A
返回
ID YourCol DeDuped
1 qw"qw"er"er qw"er
2 q"w"w"q q"w
3 f"k"s"g f"k"s"g
选项 2:没有解析函数
Declare @YourTable table (ID int,YourCol varchar(50))
Insert Into @YourTable values
(1,'qw"qw"er"er'),
(2,'q"w"w"q'),
(3,'f"k"s"g')
Select A.ID
,A.YourCol
,DeDuped = Stuff((Select '"' + RetVal
From (Select RetSeq=Min(RetSeq),RetVal
From (
Select RetSeq = Row_Number() over (Order By (Select null))
,RetVal = LTrim(RTrim(B.i.value('(./text())[1]', 'varchar(max)')))
From (Select x = Cast('<x>' + replace((Select replace(A.YourCol,'"','§§Split§§') as [*] For XML Path('')),'§§Split§§','</x><x>')+'</x>' as xml).query('.')) as A
Cross Apply x.nodes('x') AS B(i)
) P1
Group By RetVal) P
Order by RetSeq
For XML Path('')),1,1,'')
From @YourTable A
返回
ID YourCol DeDuped
1 qw"qw"er"er qw"er
2 q"w"w"q q"w
3 f"k"s"g f"k"s"g
UDF(如果有兴趣)
CREATE FUNCTION [dbo].[udf-Str-Parse] (@String varchar(max),@Delimiter varchar(10))
Returns Table
As
Return (
Select RetSeq = Row_Number() over (Order By (Select null))
,RetVal = LTrim(RTrim(B.i.value('(./text())[1]', 'varchar(max)')))
From (Select x = Cast('<x>' + replace((Select replace(@String,@Delimiter,'§§Split§§') as [*] For XML Path('')),'§§Split§§','</x><x>')+'</x>' as xml).query('.')) as A
Cross Apply x.nodes('x') AS B(i)
);
--Thanks Shnugo for making this XML safe
--Select * from [dbo].[udf-Str-Parse]('Dog,Cat,House,Car',',')
--Select * from [dbo].[udf-Str-Parse]('John Cappelletti was here',' ')
--Select * from [dbo].[udf-Str-Parse]('this,is,<test>,for,< & >',',')
--Performance On a 5,000 random sample -8K 77.8ms, -1M 79ms (+1.16), -- 91.66ms (+13.8)
关于sql - SQL中如何去除字符串中的重复项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42738165/