简体   繁体   English

如何在格式不同的字段上连接两个表

[英]How to join two tables on fields that are not formatted the same

I have two tables containing a string field that in one table is six bytes long, and in the other table is seven bytes long, because of the insertion of a hyphen between the first two bytes and the last four bytes.我有两个包含一个字符串字段的表,其中一个表长六个字节,另一个表长七个字节,因为在前两个字节和最后四个字节之间插入了一个连字符。 The two fields look like this: AB1234 and AB-1234.这两个字段如下所示:AB1234 和 AB-1234。 I tried a join like this:我尝试了这样的连接:

    FROM       TableA ta
    INNER JOIN TableB tb ON Left(ta.fld, 2) + '-' + Mid(ta.fld, 3) = tb.fld

...and I tried ...我试过了

    FROM       TableA ta
    INNER JOIN TableB tb ON Left(ta.fld, 2) = Left(tb.fld, 2) AND Mid(ta.fld, 3) = Mid(tb.fld, 4)

...but neither works. ...但两者都不起作用。 Is there a way to do this with a subquery?有没有办法用子查询来做到这一点? Is there another approach?还有另一种方法吗?

How about using REPLACE to remove the dash before comparing:如何在比较之前使用REPLACE删除破折号:

SELECT *
FROM TableA a
INNER JOIN TableB b
    ON a.fld = REPLACE(b.fld, "_", "");

使用 RIGHT 而不是 MID

Left(ta.fld, 2) + '-' + RIGHT(ta.fld, 4) = tb.fld

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

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