locked
Ho to Compare two almost similar String RRS feed

  • Question

  • Below is two columns from two table which is almost identical . How can i compare these strings so i get 

    Status as True.




    • Edited by Saritask Monday, June 29, 2020 1:26 PM
    Monday, June 29, 2020 1:21 PM

Answers

  • Hi Saritask,

    If you can provide when it is true and when it is false, it is helpful to solve the problem.
    You can consider writing a function to judge the similarity. The following example may be helpful to you
    CREATE FUNCTION strcompare 
    (@StrColumnA AS VARCHAR(255) , 
    @StrColumnB AS VARCHAR(255)) 
    RETURNS INT 
    AS 
    BEGIN 
    DECLARE @Result INT 
    DECLARE @lenA INT 
    DECLARE @lenB INT 
    DECLARE @lenSameA INT 
    DECLARE @lenSameB INT 
    SET @lenA=LEN(@StrColumnA) 
    SET @lenB=LEN(@StrColumnB) 
    --Calculate the number of characters in A that exist in B
    SELECT @lenSameA=COUNT(*) 
    FROM test 
    WHERE @StrColumnB LIKE '%'+StrColumnA+'%' 
    --Count the number of characters in B that exist in A
    SELECT @lenSameB=COUNT(*) 
    FROM test
    WHERE @StrColumnA LIKE '%'+StrColumnB+'%' 
    SET @Result=(@lenSameA+@lenSameB)*100/(@lenA+@lenB) 
    RETURN @Result 
    END

     
    Best Regards, 
    Echo 

    ------------------------------

    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com



    • Edited by Echo Liuz Tuesday, June 30, 2020 8:27 AM
    • Proposed as answer by Echo Liuz Thursday, July 2, 2020 1:34 AM
    • Marked as answer by Saritask Monday, July 6, 2020 4:45 PM
    Tuesday, June 30, 2020 7:59 AM

All replies

  • Hi, I hope below link will resolve the string compare https://www.w3resource.com/mysql/string-functions/mysql-strcmp-function.php#:~:text=MySQL%20strcmp()%20function%20is,is%20smaller%20the%20first%20one.&text=First%20string%20for%20comparison.&text=Second%20string%20for%20comparison.
    Monday, June 29, 2020 1:30 PM
  • In your example status is always false; when should it be true and how would you define "similar" here?

    Olaf Helper

    [ Blog] [ Xing] [ MVP]

    Monday, June 29, 2020 1:41 PM
  • Hi olaf, All are similar. It is coming as False in my query
    Monday, June 29, 2020 1:59 PM
  • There is Microsoft supplied database that implements several great CLR functions including RegEX and string comparison algorithms, but I forgot the name of it right now and could not find doing Google Searches, can someone please refresh my memory?

    I was able to find this

    https://igormicev.com/regex-in-sql-server-for-searching-texts/

    but this is not what I was searching for.

    Also found this one

    https://www.sqlsharp.com/features/

    which is quite interesting and may help (not the one I was searching for).

    Also found this interesting approach https://www.red-gate.com/simple-talk/sql/t-sql-programming/fuzzy-searches-sql-server/


    For every expert, there is an equal and opposite expert. - Becker's Law


    My blog


    My TechNet articles



    • Edited by Naomi N Monday, June 29, 2020 6:43 PM
    • Proposed as answer by Echo Liuz Thursday, July 2, 2020 1:34 AM
    Monday, June 29, 2020 2:35 PM
  • Ok, several more searches and I figured out the name of the MS supplied DB mdq and found this article

    https://www.decisivedata.net/blog/cleaning-messy-data-sql-part-1-fuzzy-matching-names

    but so far I didn't find the link to download the database itself yet.


    For every expert, there is an equal and opposite expert. - Becker's Law


    My blog


    My TechNet articles

    Monday, June 29, 2020 6:56 PM
  • hi

    can show insert statements of above data storage columns

    Thanks and Regards

    Laxmidhar sahoo

    Monday, June 29, 2020 7:10 PM
  • Hi Saritask,

    If you can provide when it is true and when it is false, it is helpful to solve the problem.
    You can consider writing a function to judge the similarity. The following example may be helpful to you
    CREATE FUNCTION strcompare 
    (@StrColumnA AS VARCHAR(255) , 
    @StrColumnB AS VARCHAR(255)) 
    RETURNS INT 
    AS 
    BEGIN 
    DECLARE @Result INT 
    DECLARE @lenA INT 
    DECLARE @lenB INT 
    DECLARE @lenSameA INT 
    DECLARE @lenSameB INT 
    SET @lenA=LEN(@StrColumnA) 
    SET @lenB=LEN(@StrColumnB) 
    --Calculate the number of characters in A that exist in B
    SELECT @lenSameA=COUNT(*) 
    FROM test 
    WHERE @StrColumnB LIKE '%'+StrColumnA+'%' 
    --Count the number of characters in B that exist in A
    SELECT @lenSameB=COUNT(*) 
    FROM test
    WHERE @StrColumnA LIKE '%'+StrColumnB+'%' 
    SET @Result=(@lenSameA+@lenSameB)*100/(@lenA+@lenB) 
    RETURN @Result 
    END

     
    Best Regards, 
    Echo 

    ------------------------------

    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com



    • Edited by Echo Liuz Tuesday, June 30, 2020 8:27 AM
    • Proposed as answer by Echo Liuz Thursday, July 2, 2020 1:34 AM
    • Marked as answer by Saritask Monday, July 6, 2020 4:45 PM
    Tuesday, June 30, 2020 7:59 AM
  • Hi olaf, All are similar. It is coming as False in my query
    And how does your query look like?

    Olaf Helper

    [ Blog] [ Xing] [ MVP]

    Tuesday, June 30, 2020 8:29 AM
  • Hi Saritask,
    Has your problem been solved? If it is solved, please mark the point that you 
    think is correct as an answer. This can help others who encounter similar problems.

    Best Regards
    Echo

    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com

    Monday, July 6, 2020 7:05 AM