我正在尝试从地址中分离门牌号码和街道名称。
我能够拆分大多数地址,直到我获得一个包含数字和字母作为HouseNumber一部分的地址。
我已经在下面尝试过了,它在大多数情况下都有效。但这对 876A Mountain Breeze Rd 这样的地址不起作用。我无法从其余地址中分割房屋号 876A 。
他们是否可以将 876A 拆分为[门牌号]?
SELECT
C.CustomerID
,CASE
WHEN ISNUMERIC(LEFT(LTRIM(C.ServiceAddress), CHARINDEX(' ',
LTRIM(C.ServiceAddress)))) = 1 THEN
LEFT(LTRIM(C.ServiceAddress), CHARINDEX(' ',
LTRIM(C.ServiceAddress)))
ELSE
NULL
END AS [House Number]
,CASE
WHEN ISNUMERIC(LEFT(LTRIM(C.ServiceAddress), CHARINDEX(' ', LTRIM(C.ServiceAddress)))) = 1 THEN
LTRIM(RTRIM(SUBSTRING(
LTRIM(C.ServiceAddress),
CHARINDEX(' ', LTRIM(C.ServiceAddress)) + 1,
LEN(LTRIM(C.ServiceAddress)))))
ELSE
LTRIM(RTRIM(C.ServiceAddress))
END AS [Street Name]
FROM dbo.Customers AS C
答案 0 :(得分:1)
您可以尝试一下,看看是否能给您正确的结果吗?
SELECT
C.CustomerID
,CASE
WHEN ISNUMERIC(LEFT(LTRIM(C.ServiceAddress), CHARINDEX(' ',
LTRIM(C.ServiceAddress)))) = 1 OR ISNUMERIC(LEFT(LTRIM(C.ServiceAddress), CHARINDEX('
',
LTRIM(C.ServiceAddress)))) = 0 THEN
LEFT(LTRIM(C.ServiceAddress), CHARINDEX(' ',
LTRIM(C.ServiceAddress)))
ELSE
NULL
END AS [House Number]
,CASE
WHEN ISNUMERIC(LEFT(LTRIM(C.ServiceAddress), CHARINDEX(' ',
LTRIM(C.ServiceAddress)))) = 1 OR ISNUMERIC(LEFT(LTRIM(C.ServiceAddress), CHARINDEX('
', LTRIM(C.ServiceAddress)))) = 0 THEN
LTRIM(RTRIM(SUBSTRING(
LTRIM(C.ServiceAddress),
CHARINDEX(' ', LTRIM(C.ServiceAddress)) + 1,
LEN(LTRIM(C.ServiceAddress)))))
ELSE
LTRIM(RTRIM(C.ServiceAddress))
END AS [Street Name]
FROM dbo.Customers AS C