SQL: Join tables on substrings

前端 未结 5 760
北海茫月
北海茫月 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条回答
  •  慢半拍i
    慢半拍i (楼主)
    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

提交回复
热议问题