Sql String search

Rajapriya Rajendran 21 Reputation points
2021-04-27T06:58:04.323+00:00

Hi,

I have the below result set from query 1. Now I need to count the key when the names are totally different for the same key (as highlighted below) if they are partially same (for ex siddharth and Sid) then we don’t need to count them.

91568-82f06aaa-6aeb-4702-8515-296574d87197.jpeg

Developer technologies Transact-SQL
SQL Server Other
{count} votes

1 answer

Sort by: Most helpful
  1. MelissaMa-MSFT 24,221 Reputation points
    2021-04-27T07:12:45.807+00:00

    Hi @Rajapriya Rajendran ,

    Welcome to Microsoft Q&A!

    Please refer below example and check whether it is working.

    drop table if exists Search  
      
    create table Search  
    (name varchar(100),  
    [Thekey] int)  
      
    insert into Search values  
    ('Blum,Kaye',294426789),  
    ('BLUM,KAYE ELIZABETH',294426789),  
    ('BAKER,MICHELLE',294739049),  
    ('BOOT,DAMEN',294739049)  
      
    select count(distinct a.[Thekey]) count  
    from Search a  
    left join Search b   
    on a.[Thekey]=b.[Thekey]   
    where a.name not like '%'+b.name+'%'   
    and b.name not like '%'+a.name+'%'   
    

    OR

     select count(distinct a.[Thekey]) count  
     from Search a  
     left join Search b   
     on a.[Thekey]=b.[Thekey]   
     where CHARINDEX(a.name,b.name)=0   
     and CHARINDEX(b.name,a.name)=0  
    

    If above is not working, please provide more sample data, details of rule and expected output.

    Best regards
    Melissa


    If the answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.