好的,这是一个困难的。我会用代表公寓的不同字符串来创建一个表格,如下所示:
INSERT INTO ApartmentsStrings
VALUES (N'apt'),(N'apartment');
然后我会做以下事情:
SELECT A.[Address] OriginalAddress,
CASE WHEN B.Position IS NOT NULL
THEN LEFT(A.[Address],B.Position-1)
ELSE A.[Address] END Address_1,
CASE WHEN B.Position IS NOT NULL
THEN SUBSTRING(A.[Address],B.Position,LEN(A.[Address])-B.Position+1) END Address_2
FROM Address A
OUTER APPLY(SELECT PATINDEX('%'+String+'%',A.[Address]) Position
FROM ApartmentsStrings
WHERE PATINDEX('%'+String+'%',A.[Address]) > 0) B
结果:
╔═════════════════════════════════╦══════════════════════╦═══════════════╗
║ OriginalAddress ║ Address_1 ║ Address_2 ║
╠═════════════════════════════════╬══════════════════════╬═══════════════╣
║ 5235 Georgia Street Apt 8 ║ 5235 Georgia Street ║ Apt 8 ║
║ 436 Oregon Avenue Apartment # 5 ║ 436 Oregon Avenue ║ Apartment # 5 ║
╚═════════════════════════════════╩══════════════════════╩═══════════════╝
当定义公寓的字符串包含在另一个字符串中时(即:apart
和apartment
),您将有重复。误报的可能性也很大,这意味着您可以拥有包含apt
但并不意味着公寓的地址。
这是一个sqlfiddle 供您尝试。