SQL: Join tables on substrings

前端 未结 5 737
北海茫月
北海茫月 2020-12-11 05:56

I have a table A with the string-column a and a table B with the string-column b. a is a substring of b.

相关标签:
5条回答
  • 2020-12-11 06:27
    declare @tmp1 table (id int, a varchar(max))
    declare @tmp2 table (id int, b varchar(max))
    
    
    insert into @tmp1 (id, a) values (1,'one')
    insert into @tmp2 (id,b) values (1,'onetwo')
    
    select * from @tmp1 one inner join @tmp2 two on charindex(one.a,two.b) > 0
    

    You can also use charindex, 0 means its not found, greater than 0 is the start index

    charindex

    0 讨论(0)
  • 2020-12-11 06:30

    You can use like

    select *
    from A
      inner join B 
        on B.b like '%'+A.a+'%'
    
    0 讨论(0)
  • 2020-12-11 06:45

    set an inner join on a substring(4 letters) of FIELD1 of table TABLE1 with FIELD1 of table TABLE2

    select TABLE1.field1,TABLE2.field1 from TABLE1 inner join TABLE2 on substring(TABLE1.field1,2,5)=TABLE2.field1
    
    0 讨论(0)
  • 2020-12-11 06:49

    try this:

    Select * from A,B where B.b LIKE '%'+A.a+'%'
    
    0 讨论(0)
  • 2020-12-11 06:51

    You have the contains function: http://msdn.microsoft.com/en-us/library/ms187787.aspx

    select * from A,B where contains(B.b, A.a)
    
    0 讨论(0)
提交回复
热议问题