question

RajapriyaRajendran-3296 avatar image
0 Votes"
RajapriyaRajendran-3296 asked MelissaMa-msft commented

Sql String search

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


sql-server-generalsql-server-transact-sql
· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

if they are partially same (for ex siddharth and Sid) then we don’t need to count them.

In database world there is not really something like "partially same"; data equals or not.
Please post some sample data as T-SQL script and the expected result.



0 Votes 0 ·

Hi @RajapriyaRajendran-3296,

Could you please provide any update? Thanks.

Best regards
Melissa

0 Votes 0 ·

1 Answer

MelissaMa-msft avatar image
0 Votes"
MelissaMa-msft answered MelissaMa-msft edited

Hi @RajapriyaRajendran-3296,

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.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.