匹配oracle sql中的字符where条件

时间:2016-06-02 19:15:33

标签: oracle

我希望将表a中的Col1与表B中的colum1相匹配。

A       B
123     123-ab
234     234-bc
3443    3443-dd

但是,表b中的值具有连接数据。我想只匹配字符,直到出现特殊字符( - )。 我试过这个:substr(table1.a,1,3) = substr(table2.b,1,3)

但这不起作用,因为有些值有4位数。

2 个答案:

答案 0 :(得分:1)

使用join和substr

 select * from table_a
 inner join  table_b on table_a.col_a = substr(table_b.col_b, 1, length(table_a.col_a); 

答案 1 :(得分:0)

使用REGEXP_SUBSTR()匹配字符串开头的一个或多个数字,但不包括第一个连字符:

SQL> with a(col1) as (
     select '123'  from dual union
     select '234'  from dual union
     select '3443' from dual
   ),
   b(col1) as (
     select '123-ab'  from dual union
     select '234-bc'  from dual union
     select '3443-dd' from dual
   )
   select a.col1, b.col1
   from a, b
   where a.col1 = regexp_substr(b.col1, '^(\d+)-', 1, 1, NULL, 1);

COL1 COL1
---- -------
123  123-ab
234  234-bc
3443 3443-dd

SQL>