1

I'm trying to check for duplicate bame in SQL Server.

Table Company:

   CompanyName            | Business
   -----------------------+----------
   Example Company INC    | Telecom 
   The Example Company    | Telecom

I want to get the both of the company name as duplicate. using the parameters "The Example Company INC".

When I use

SELECT CompanyName 
FROM COMPANY 
WHERE CompanyName LIKE '%The Example Company INC%' 

it show no results. The query must return 2 records - is this possible?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Alvie
  • 29
  • 6

2 Answers2

1

If this is the only case where one company is 'Example Company INC' and another company is 'The Example Company' then the following query can work

SELECT CompanyName FROM COMPANY WHERE CompanyName LIKE '%Example Company%'

But i think there can be many companies with different names. So in this case, I think for you the best is to find the similarity between two strings. And on the base of similarity factor you can decide whether they are same or not.

If your case is second one then this post can help you

Community
  • 1
  • 1
Abbas
  • 763
  • 1
  • 12
  • 26
  • The second one is correct. I need to find if the string are somewhat similar. On the example i have given the words "The" and "INC" are making the string unique. – Alvie Jan 04 '17 at 05:11
0

Split the string and Go with Dynamic coding

DECLARE @QRY VARCHAR(MAX), @DIVISIONS  VARCHAR(MAX)='The Example Company INC';

    DECLARE @START INT=1, @END INT=LEN(@DIVISIONS)
    , @LIKE VARCHAR(MAX)='CompanyName LIKE ''%';

    WHILE (@START<=@END)
    BEGIN

    SELECT @LIKE =@LIKE+  CASE ASCII(SUBSTRING(@DIVISIONS, @START, 1))
        WHEN 32
            THEN '%'' OR CompanyName LIKE ''%'
        ELSE SUBSTRING(@DIVISIONS, @START, 1)
        END

    SET @START+=1

    END
    SET @LIKE = @LIKE+'%'''

    SELECT @QRY = ' SELECT CompanyName FROM COMPANY 
    WHERE ' + @LIKE

    PRINT @QRY


    EXEC(@QRY)

Another simple way of Dynamic Query:

DECLARE @QRY VARCHAR(MAX), @DIVISIONS  VARCHAR(MAX)='The Example Company INC'
,@LIKE VARCHAR(MAX)='CompanyName LIKE ''%';

DECLARE @END INT=LEN(@DIVISIONS)
select @LIKE = @LIKE+   CASE ASCII(SUBSTRING(@DIVISIONS, number, 1))
        WHEN 32
        THEN '%'' OR CompanyName LIKE ''%'
ELSE SUBSTRING(@DIVISIONS, number, 1)
END
FROM master.dbo.spt_values 
where type='p' and number between 1 and @END

SET @LIKE = @LIKE+'%'''
SELECT @QRY = ' SELECT CompanyName FROM COMPANY 
WHERE ' + @LIKE

PRINT @QRY


EXEC(@QRY)
Shakeer Mirza
  • 5,054
  • 2
  • 18
  • 41