SQL Server:在英国邮政编码单位字段中的字符之间搜索

时间:2016-04-18 18:03:19

标签: sql-server

作为邮政编码查询的解决方案,我将邮政编码数据库拆分为区域,区域,区域,单位的单独列。我甚至将像W1A和W1B等中的A和B分开的角色分开。

我想搜索像B46 3BA这样的邮政编码,并且返回的详细信息如下所示。

Sql Server Manager View

到目前为止使用的SQL查询是

SELECT     
    StationID, FromPostcode, ToPostcode, FromPostCodeArea, 
    FromPostCodeDistrict, FromPostCodeDistrictChar, 
    FromPostCodeSector, FromPostCodeUnit,
    ToPostCodeArea, ToPostCodeDistrict, 
    ToPostCodeDistrictChar, ToPostCodeSector, ToPostCodeUnit
FROM
    Station
WHERE     
    (FromPostCodeArea = 'B') 
    AND (3 BETWEEN FromPostCodeSector AND ToPostCodeSector) 
    AND (46 BETWEEN FromPostCodeDistrict AND ToPostCodeDistrict)

由于遗留应用程序原因,我的主要问题是我无法访问Select查询,只能更改WHERE子句。我只是需要它来返回这个案例行中的单行与stationid 3321。

但我完全迷失了如何进一步采取这一点

答案正如Jamie所建议的那样使用sql

SELECT     
    StationID, FromPostcode, ToPostcode, FromPostCodeArea, 
    FromPostCodeDistrict, FromPostCodeDistrictChar, 
    FromPostCodeSector, FromPostCodeUnit,
    ToPostCodeArea, ToPostCodeDistrict, 
    ToPostCodeDistrictChar, ToPostCodeSector, ToPostCodeUnit
FROM
Station
WHERE     
    (FromPostCodeArea = 'B') 
    AND (3 BETWEEN FromPostCodeSector AND ToPostCodeSector) 
    AND (46 BETWEEN FromPostCodeDistrict AND ToPostCodeDistrict)
    AND ('B46 3BA' BETWEEN FromPostCode AND ToPostCode)

似乎在2000个随机邮政编码的测试运行中做了这个技巧。

0 个答案:

没有答案