如何才能加入选定的文字?

时间:2012-04-24 17:57:17

标签: sql sql-server-2008 tsql

我有两张桌子。我需要使用substring函数连接这些表。我试过这样的事。但它不起作用......

Table01

ID  NameStreet
1   Julian Assange 100 Majestic street, Toronto
2   Brad Pit 200 Loaden Blvd, Toronto
3   Allen Sunderland 9, Unit 2, Janet Street, BC
4   Julian Assange 50 Majectic street, Toronto

TABLE02

ID Name
1  Julian Assange
2  Allen Sunderland 
3  Julian Assange

当前查询(不工作)

SELECT t1.* 
FROM Table01 t1 
JOIN Table02 t2 
ON t2.Name = SUBSTRING(t1.NameStreet, 0, CHARINDEX(t2.Name, t1.NameStreet))

期待结果: -

1   Julian Assange 100 Majestic street, Toronto
3   Allen Sunderland 9, Unit 2, Janet Street, BC
4   Julian Assange 50 Majectic street, Toronto

1 个答案:

答案 0 :(得分:0)

我认为你在查询中想要这个:

on t2.Name = LEFT(t1.NameStreet, (charindex(' ', t1.NameStreet)-1) )

修改

这有用吗?

on t2.Name = LEFT(t1.NameStreet, Len(t2.Name) )