简体   繁体   中英

Exclude special characters and extract a set of numbers in T-SQL

I am trying to join 2 tables. The only common thing between the 2 table are the last set of the numbers (eg after the last 2 tilde ~~) in the first table; however the last set of numbers are kind of irregular, so I was not able just right () to get the last set of numbers. Could anyone shed some light on making the joining work? Thank you so much.

The first table looks like this:

+------------+----------------------------+
|  Indent    |           Code 1           | 
+------------+----------------------------+
|    1       |  11-21                     |
+------------+----------------------------+
|    2       |  11-21~~11-22              |
+------------+----------------------------+
|    3       |  11-21~~11-22~~11-22-33    |
+------------+----------------------------+
|    3       |  11-21~~11-22~~11-22-3355  |
+------------+----------------------------+

The second table is like this:

+------------+----------------------------+
|  Item Name |          Code 2            |
+------------+----------------------------+
|    A       |  11-21                     |
+------------+----------------------------+
|    B       |  11-22                     |
+------------+----------------------------+
|    C       |  11-22-33                  |
+------------+----------------------------+
|    D       |  11-22-3355                |
+------------+----------------------------+

And I expect the result like this:

+------------+----------------------------+-------------+-------------+
|  Indent    |           Code 1           |  Code 2     | Item Name   |
+------------+----------------------------+-------------+-------------+
|    1       |  11-21                     |  11-21      |     A       |
+------------+----------------------------+-------------+-------------+
|    2       |  11-21~~11-22              |  11-22      |     B       |
+------------+----------------------------+-------------+-------------+
|    3       |  11-21~~11-22~~11-22-33    |  11-22-33   |     C       |
+------------+----------------------------+---------------------------+
|    3       |  11-21~~11-22~~11-22-3355  |  11-22-33-55|     D       |
+------------+----------------------------+-------------+-------------+

You can use a pair of conditions in the join to handle the somewhat erratic matching rules:

select TFT.Indent, TFT.[Code 1], TST.[Code 2], TST.[Item Name]
  from TheFirstTable as TFT inner join
    TheSecondTable as TST on
      -- An exact match between codes or ...
      TFT.[Code 1] = TST.[Code 2] or
      -- ... Code 2 is at the end of Code 1 after a pair of tildes.
      TFT.[Code 1] like '%~~' + TST.[Code 2]
-- In Oracle
SELECT a.Ident, a.Code1, b.Code2, b.ItemName 
  FROM table1 a
  INNER JOIN table2 b ON INSTR(a.code1, b.code2) > 0 ;

-- In SQL Server
SELECT a.Ident, a.Code1, b.Code2, b.ItemName 
  FROM table1 a
  INNER JOIN table2 b ON a.code1 LIKE '%'+ b.code2 +'%' ;

-- Or Usin Concat
SELECT a.Ident, a.Code1, b.Code2, b.ItemName 
  FROM table1 a
  INNER JOIN table2 b ON a.code1 LIKE CONCAT('%', b.code2 ,'%');

-- In MySQL
SELECT a.Ident, a.Code1, b.Code2, b.ItemName 
  FROM table1 a 
  INNER JOIN table2 b ON a.code1 LIKE CONCAT('%', b.code2, '%');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM