我正在尝试查找表视图中的所有地址,这些地址属于某个邮政编码列表(500 + zipcodes)
通过不同的答案搜索,我发现了一些与我在this StackPost
中寻找的东西非常接近的东西问题是我正在处理一个视图(没有全文搜索),而不是一对一的值。
我尝试过按照以下方式做的事情:
set @sql = "SELECT * FROM viewTABLE
WHERE CONTAINS(Address, '"+replace(@list, ',', ' OR ')+"')"
但如上所述,这不起作用 - 我可以创建一个包含邮政编码列表的表,如果这样可以更容易,但每个列的“地址”都必须与邮政编码列表进行比较。
非常感谢任何帮助!
答案 0 :(得分:0)
您可以使用LIKE
声明修改现有代码以执行此类操作:
set @sql="
SELECT *
FROM viewTABLE
WHERE Address LIKE '%"+replace(@list, ",", "%' OR Address LIKE '%")+"%'"
会生成像这样的@sql:
SELECT *
FROM viewTABLE
WHERE Address LIKE '%12345%' OR Address LIKE '%22345%' OR Address Like '%55314%'
-- etc.
然而,如果zipcodes列表在表格中会更容易:
SELECT *
FROM viewTABLE v
WHERE EXISTS
(SELECT * FROM zipCodeList z
WHERE v.Address LIKE '%'+z.ZipCode+'%')
答案 1 :(得分:0)
这不是答案,但这是一个开始,帮助您了解我的想法,以帮助您解决问题。我设法隔离地址列开头的数值。如果你隔离了最后一个字符,那么你就在路上了。
DECLARE @Customer TABLE
(
NewPK INT IDENTITY(1, 1) ,
CustomerAddress NVARCHAR(MAX) NOT NULL
)
INSERT INTO @Customer
( CustomerAddress
)
VALUES ( '10 Fraley St # C, Kane, PA 16735'
)
INSERT INTO @Customer
( CustomerAddress
)
VALUES ( '5611 Market St, Maru, PA 500'
)
INSERT INTO @Customer
( CustomerAddress
)
VALUES ( '5644 N County Road 400 N, Scipio, IN 47273'
)
INSERT INTO @Customer
( CustomerAddress
)
VALUES ( '79 Fraley St # A, Kane, PA 500'
)
INSERT INTO @Customer
( CustomerAddress
)
VALUES ( '5195 N Camelot Way, North Vernon, IN 47265'
)
INSERT INTO @Customer
( CustomerAddress
)
VALUES ( '79 Fraley St # A, Kane, PA 16735'
)
SELECT CustomerAddress ,
CASE WHEN PATINDEX('%[^0-9]%', CustomerAddress) > 0
THEN SUBSTRING(CustomerAddress,
PATINDEX('%[0-9]%', CustomerAddress),
CHARINDEX(' ', CustomerAddress + SPACE(1),
PATINDEX('%[0-9]%', CustomerAddress)))
ELSE CustomerAddress
END AS 'numeric'
FROM @Customer