0

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

表01

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

表02

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
4

1 回答 1

0

我认为您希望在查询中这样做:

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

编辑

这行得通吗?

on t2.Name = LEFT(t1.NameStreet, Len(t2.Name) )
于 2012-04-24T18:03:13.420 回答