Sqlserver
 sql >> Teknologi Basis Data >  >> RDS >> Sqlserver

Cara menghapus duplikat dari string di SQL

Opsi 1 dengan Fungsi Parse

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

Pengembalian

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

Opsi 2:Tanpa Fungsi Parse

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

Pengembalian

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 jika Tertarik

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)



  1. Database
  2.   
  3. Mysql
  4.   
  5. Oracle
  6.   
  7. Sqlserver
  8.   
  9. PostgreSQL
  10.   
  11. Access
  12.   
  13. SQLite
  14.   
  15. MariaDB
  1. Batasan yang Dapat Ditangguhkan di SQL Server

  2. Cara Menginstal SQL Server di Mac dengan VirtualBox

  3. tsql:Cara mengambil tanggal terakhir setiap bulan antara rentang tanggal yang diberikan

  4. Ekstrak nilai angka pertama dari string sql

  5. Server SQL Pivot Multi Kolom