我有一个地址列,邮政编码中的值为+
。但是+可以在列中的任何地方(街道名称,城市),但是我只需要用另一个称为Postal_Code
的列来标识地址中的邮政编码,然后删除+
。
要在邮政编码中识别
+
和 删除但不知道该怎么办?
我已经从+
列中删除了Postal_Code
。
表格示例
POSTAL_CODE FORMATTED_ADDRESS
T2E 8N6 #6, 1435-40th Ave NE #6 Calgary AB T2E+8N6 Canada
23401 9 Abolaji Street Mafoluku +23401 Oshodi Lagos State Nigeria
234-01 6 Force Road, Square Race Course +234-01 Lagos, Nigeria Lagos State
86 163453 Daqing Oilfield Co.Ltd Ranghulu 86+163453 Daqing CHN
答案 0 :(得分:2)
这在MS SQL Server中似乎很难。
(使用regexp_replace函数会容易得多...)
反正......
示例片段:
declare @YourTable table (POSTAL_CODE varchar(30), FORMATTED_ADDRESS varchar(1000));
insert into @YourTable (POSTAL_CODE, FORMATTED_ADDRESS) values
('T2E 8N6','#6, 1435-40th Ave NE #6 Calgary AB T2E+8N6 Canada'),
('23401', '9 Abolaji Street Mafoluku +23401 Oshodi Lagos State Nigeria'),
('234-01', '6 Force Road, Square Race Course +234-01 Lagos, Nigeria Lagos State'),
('86 163453', 'Daqing Oilfield Co.Ltd Ranghulu 86+163453 Daqing CHN'),
(NULL, 'foo +1234 bar'),
('12345', 'bar 12345+ baz');
UPDATE @YourTable
SET FORMATTED_ADDRESS =
REPLACE(
REPLACE(
REPLACE(FORMATTED_ADDRESS,
'+'+POSTAL_CODE, POSTAL_CODE),
POSTAL_CODE+'+', POSTAL_CODE),
REPLACE(POSTAL_CODE,' ','+'), POSTAL_CODE)
WHERE POSTAL_CODE IS NOT NULL
AND (
FORMATTED_ADDRESS LIKE CONCAT('%+', POSTAL_CODE, '%') OR
FORMATTED_ADDRESS LIKE CONCAT('%', POSTAL_CODE, '+%') OR
(POSTAL_CODE LIKE '%[ ]%' AND FORMATTED_ADDRESS LIKE CONCAT('%', REPLACE(POSTAL_CODE,' ','+'), '%'))
);
SELECT * FROM @YourTable;
答案 1 :(得分:0)
这可以通过一些REPLACE
语句来完成:
UPDATE YourTable
SET FORMATTED_ADDRESS = REPLACE(
REPLACE(FORMATTED_ADDRESS, REPLACE(POSTAL_CODE, ' ', '+'), POSTAL_CODE),
'+'+POSTAL_CODE,
POSTAL_CODE)
FROM YourTable
您可以先SELECT
来测试此查询的结果:
SELECT REPLACE(
REPLACE(FORMATTED_ADDRESS, REPLACE(POSTAL_CODE, ' ', '+'), POSTAL_CODE),
'+'+POSTAL_CODE,
POSTAL_CODE) AS NEW_FORMATTED_ADDRESS,
FORMATTED_ADDRESS
FROM YourTable