Click here to Skip to main content
15,860,972 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I work on sql server 2019 i have issue i can't get percentage of two string from table

so i will compare between two string columns (PartText,MaskText)
and i will display similar percentage of two column

so as sample 'TR00123907FG','KB00123907FG' two string different only on first two charachters

so difference will be 80 percent

create table #compareTextPercentage
 (
 PartText varchar(50),
 MaskText varchar(50)
 )
 insert into #compareTextPercentage(PartText,MaskText)
 values
 ('TR00123907FG','KB00123907FG'),
 ('TR00123907FG','TR00123907FG'),
 ('klmhedf43122','50ghlpnkyzhy')


expected result

What I have tried:

i try using difference but not give me accurate result
DECLARE @tbl table (ID INT IDENTITY PRIMARY KEY,  PartText varchar(50),  MaskText varchar(50));
 INSERT INTO @tbl (PartText,MaskText) VALUES
  ('TR00123907FG','KB00123907FG'),
  ('TR00123907FG','TR00123907FG'),
  ('klmhedf43122','50ghlpnkyzhy');
    
 SELECT * 
  , Diff = DIFFERENCE(PartText, MaskText) * 25
 FROM @tbl;
Posted
Updated 1-Nov-22 20:15pm

1 solution

You will have to write your own function: DIFFERENCE returns an integer between 0 and 4 inclusive, which means the only values you can get from your code are 0%, 25%, 50%, 75%, and 100%.
SQL Server DIFFERENCE() Function[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900