sql >> データベース >  >> RDS >> Sqlserver

SQL の文字列から重複を削除する方法

    解析関数を使用したオプション 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)
    



    1. MariaDBクラスターにmaxctrlを使用したMaxScale管理の概要

    2. mysql全文検索が3文字で機能しない

    3. 重複エントリのクリーンアップ更新

    4. SQL Server 2012 全文検索をインストールまたは追加する