简体   繁体   English

在 T-SQL 中排除特殊字符并提取一组数字

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

I am trying to join 2 tables.我正在尝试加入 2 张桌子。 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; 2 个表之间唯一的共同点是第一个表中的最后一组数字(例如在最后 2 个波浪号 ~~ 之后); 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:您可以在join使用一对条件来处理有些不稳定的匹配规则:

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, '%');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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