SQL Server 2008 R2: Matching strings

廉价感情. 提交于 2019-12-11 06:48:45

问题


I have the following table:

Table:

CREATE TABLE str_matching
(
    colstr varchar(200)
);

Insert data:

INSERT INTO str_matching VALUES('5sXYZA1010B')
INSERT INTO str_matching VALUES('A1010B')
INSERT INTO str_matching VALUES('AMZ103B15K')
INSERT INTO str_matching VALUES('B15K')
INSERT INTO str_matching VALUES('XC101')
INSERT INTO str_matching VALUES('C101')
INSERT INTO str_matching VALUES('502KMD1FZ10009L')
INSERT INTO str_matching VALUES('FZ10009L')
INSERT INTO str_matching VALUES('A9L')
INSERT INTO str_matching VALUES('XZ049L')
INSERT INTO str_matching VALUES('LM101')

Expected Output: I want to display only those records that has duplicate entries, if one string match last part of any string then I am considering as duplicate.

For example: I have two strings

  1. 5sXYZA1010B
  2. A1010B

2nd string which is matching at end of 1st string, so want to display such records.

Note: Length of string's are not fixed, it can be match at any point.

Expected Result:

colstr              
--------------------
5sXYZA1010B         
A1010B              
AMZ103B15K          
B15K                
XC101               
C101                
502KMD1FZ10009L     
FZ10009L        

回答1:


This should do it (demo)

SELECT DISTINCT CA.colstr
FROM   str_matching s1
       JOIN str_matching s2
         ON s1.colstr <> s2.colstr
            AND s2.colstr LIKE '%' + s1.colstr
       CROSS APPLY (VALUES(s1.colstr),
                          (s2.colstr)) CA(colstr) 

However if str_matching has many rows performance will be poor. Adding an index on the reverse of the string can substantially improve things - as in the example below.

CREATE TABLE str_matching
(
    colstr varchar(200),
    colstr_rev AS REVERSE(colstr)
);

CREATE INDEX ix_colstr_rev on str_matching(colstr_rev)

SELECT colstr = REVERSE(CA.colstr_rev)
FROM   str_matching s1
       JOIN str_matching s2
         ON s1.colstr_rev <> s2.colstr_rev
            AND s2.colstr_rev LIKE  s1.colstr_rev + '%' 
       CROSS APPLY (VALUES(s1.colstr_rev),
                          (s2.colstr_rev)) CA(colstr_rev) 
GROUP BY CA.colstr_rev



回答2:


You can use it.

;WITH CTE AS (
    select *,RN = ROW_NUMBER() OVER (ORDER BY LEN(colstr)) from str_matching 
)
,CTE2 AS (
    SELECT T1.colstr colstr1 ,X.colstr colstr2 FROM CTE T1
        CROSS APPLY (SELECT * FROM CTE T2 WHERE T2.RN > T1.RN AND RIGHT(T2.colstr, LEN(T1.colstr)) = T1.colstr) AS X
)
SELECT colstr1 FROM CTE2 
UNION ALL 
SELECT colstr2 FROM CTE2

Result:

5sXYZA1010B         
A1010B              
AMZ103B15K          
B15K                
XC101               
C101                
502KMD1FZ10009L     
FZ10009L 



回答3:


it can be match at any point.

To match any point, use like with wildcards (%) on each side:

For multiple matches, add distinct

select l.colstr
from str_matching l
  inner join str_matching r
    on l.colstr<>r.colstr
   and (l.colstr like '%' + r.colstr +'%'
     or r.colstr like '%' + l.colstr +'%'
      )

rextester demo: http://rextester.com/ICIKJ2256

returns:

+-----------------+
|     colstr      |
+-----------------+
| A1010B          |
| 5sXYZA1010B     |
| B15K            |
| AMZ103B15K      |
| C101            |
| XC101           |
| FZ10009L        |
| 502KMD1FZ10009L |
+-----------------+

Given Martin Smith's answer, I'm guessing I misunderstood your intent behind 'match at any point', so to just match the end of the string you would not use the trailing + '%'



来源:https://stackoverflow.com/questions/46667373/sql-server-2008-r2-matching-strings

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!