如何从一列中提取 postcode/digits 并将其放入 SQL Oracle 9i 中的另一列
How to extract postcode/digits from one column and put it in to another column in SQL Oracle 9i
Table ADRESSES
包含列 [ ID, STREET, TOWN, POSTCODE,COUNTRY ]
.
Oracle 9i 数据库中的热门语句:
如果第 COUNTRY
列包含字符串 'UK'
,并且 POSTCODE
为空,但第 TOWN
列以 post 代码开头(格式为 xxxxx 或 xx 的数字-xxx),然后将 postal 代码移动到 POSTCODE
列,并从 TOWN
.
列中删除 post 代码
下面有两条sql语句,一条到select,看看update语句的影响,update语句。在 运行 任何更新之前备份您的表,并始终通过 运行 将其作为 select 语句来测试更新是否按照您的预期进行。
注意:我假设您希望将邮政编码保留为最初输入的格式。例如,如果输入为 xxxxx,它将保存为 xxxxx,如果输入为 xx-xxx,它将保存为 xx-xxx。
检查更新语句将做什么:
select ID, STREET, TOWN, POSTCODE, COUNTRY,
case when substr(TOWN, 3, 1) = '-' like '%-%' then trim(substr(TOWN,7, length(TOWN)-6)) else trim(substr(TOWN,6, length(TOWN)-5)) end as NEW_TOWN,
case when substr(TOWN, 3, 1) = '-' like '%-%' then substr(TOWN, 1, 6) else substr(TOWN, 1, 5) end as NEW_POSTCODE--Assumes you want to keep the dash if it exists
from ADDRESSES
where COUNTRY like'%UK%' --contains string UK
and trim(POSTCODE) is null -- postcode is empty
and (
length(trim(translate(substr(TOWN, 1, 5), '0123456789', ' '))) is null -- town starts with xxxxx digits
or
(length(trim(translate(substr(TOWN, 1, 2)||substr(TOWN,4,3, '0123456789', ' ')))) and substr(TOWN, 3, 1) = '-') -- town starts with xx-xxx digits
)
;
如果满意,运行更新语句。
update ADDRESSES
set
TOWN = case when substr(TOWN, 3, 1) = '-' like '%-%' then trim(substr(TOWN,7, length(TOWN)-6)) else trim(substr(TOWN,6, length(TOWN)-5)) end,
POSTCODE = case when substr(TOWN, 3, 1) = '-' like '%-%' then substr(TOWN, 1, 6) else substr(TOWN, 1, 5) end --Assumes you want to keep the dash if it exists
from ADDRESSES
where COUNTRY like'%UK%' --contains string UK
and trim(POSTCODE) is null -- postcode is empty
and (
length(trim(translate(substr(TOWN, 1, 5), '0123456789', ' '))) is null -- town starts with xxxxx digits
or
(length(trim(translate(substr(TOWN, 1, 2)||substr(TOWN,4,3, '0123456789', ' ')))) and substr(TOWN, 3, 1) = '-') -- town starts with xx-xxx digits
)
;
我希望这至少可以作为一个起点。
我也没有 O9i 来测试这个,所以我怀疑我的回答将毫无用处,但我可以尝试这样做只是为了好玩。我希望以下内容能给您或其他人一个起点。以下接缝在 postgresql 9.3 中工作:-)
create table addresses(
id int,
street varchar(255),
town varchar(255),
postcode varchar(6),
country varchar(3)
);
insert into addresses values (1, '1', '11-111 dupa jasio1','', 'UK');
insert into addresses values (2, '2', '22222 dupa jasio2','', 'UK');
insert into addresses values (3, '3', '33-333 dupa jasio3','', 'UK');
insert into addresses values (4, '4', '44444 dupa jasio4','', 'UK');
insert into addresses values (5, '5', '55555 dupa jasio5','', 'PL');
dawid=# select * from addresses ;
id | street | town | postcode | country
----+--------+--------------------+----------+---------
1 | 1 | 11-111 dupa jasio1 | | UK
2 | 2 | 22222 dupa jasio2 | | UK
3 | 3 | 33-333 dupa jasio3 | | UK
4 | 4 | 44444 dupa jasio4 | | UK
5 | 5 | 55555 dupa jasio5 | | PL
查询以更新类似于 12345 的邮政编码
update addresses set
postcode=substring(town, '^([0-9]{5})'),
town=substring(town, '^[0-9]{5}\s(.*)')
where country like '%UK%' and
(postcode is null or postcode='') and
town ~ E'^[0-9]{5} ';
查询以更新类似于 12-123 的邮政编码
update addresses set
postcode=substring(town, '^([0-9]{2}\-[0-9]{3})'),
town=substring(town, '^[0-9]{2}\-[0-9]{3}\s([\w\d\-\s]+)')
where country like '%UK%' and
(postcode is null or postcode='') and
town ~ E'^[0-9]{2}\-[0-9]{3} ';
结果
dawid=# select * from addresses ;
id | street | town | postcode | country
----+--------+-------------------+----------+---------
5 | 5 | 55555 dupa jasio5 | | PL
2 | 2 | dupa jasio2 | 22222 | UK
4 | 4 | dupa jasio4 | 44444 | UK
1 | 1 | dupa jasio1 | 11-111 | UK
3 | 3 | dupa jasio3 | 33-333 | UK
Table ADRESSES
包含列 [ ID, STREET, TOWN, POSTCODE,COUNTRY ]
.
Oracle 9i 数据库中的热门语句:
如果第 COUNTRY
列包含字符串 'UK'
,并且 POSTCODE
为空,但第 TOWN
列以 post 代码开头(格式为 xxxxx 或 xx 的数字-xxx),然后将 postal 代码移动到 POSTCODE
列,并从 TOWN
.
下面有两条sql语句,一条到select,看看update语句的影响,update语句。在 运行 任何更新之前备份您的表,并始终通过 运行 将其作为 select 语句来测试更新是否按照您的预期进行。
注意:我假设您希望将邮政编码保留为最初输入的格式。例如,如果输入为 xxxxx,它将保存为 xxxxx,如果输入为 xx-xxx,它将保存为 xx-xxx。
检查更新语句将做什么:
select ID, STREET, TOWN, POSTCODE, COUNTRY,
case when substr(TOWN, 3, 1) = '-' like '%-%' then trim(substr(TOWN,7, length(TOWN)-6)) else trim(substr(TOWN,6, length(TOWN)-5)) end as NEW_TOWN,
case when substr(TOWN, 3, 1) = '-' like '%-%' then substr(TOWN, 1, 6) else substr(TOWN, 1, 5) end as NEW_POSTCODE--Assumes you want to keep the dash if it exists
from ADDRESSES
where COUNTRY like'%UK%' --contains string UK
and trim(POSTCODE) is null -- postcode is empty
and (
length(trim(translate(substr(TOWN, 1, 5), '0123456789', ' '))) is null -- town starts with xxxxx digits
or
(length(trim(translate(substr(TOWN, 1, 2)||substr(TOWN,4,3, '0123456789', ' ')))) and substr(TOWN, 3, 1) = '-') -- town starts with xx-xxx digits
)
;
如果满意,运行更新语句。
update ADDRESSES
set
TOWN = case when substr(TOWN, 3, 1) = '-' like '%-%' then trim(substr(TOWN,7, length(TOWN)-6)) else trim(substr(TOWN,6, length(TOWN)-5)) end,
POSTCODE = case when substr(TOWN, 3, 1) = '-' like '%-%' then substr(TOWN, 1, 6) else substr(TOWN, 1, 5) end --Assumes you want to keep the dash if it exists
from ADDRESSES
where COUNTRY like'%UK%' --contains string UK
and trim(POSTCODE) is null -- postcode is empty
and (
length(trim(translate(substr(TOWN, 1, 5), '0123456789', ' '))) is null -- town starts with xxxxx digits
or
(length(trim(translate(substr(TOWN, 1, 2)||substr(TOWN,4,3, '0123456789', ' ')))) and substr(TOWN, 3, 1) = '-') -- town starts with xx-xxx digits
)
;
我希望这至少可以作为一个起点。
我也没有 O9i 来测试这个,所以我怀疑我的回答将毫无用处,但我可以尝试这样做只是为了好玩。我希望以下内容能给您或其他人一个起点。以下接缝在 postgresql 9.3 中工作:-)
create table addresses(
id int,
street varchar(255),
town varchar(255),
postcode varchar(6),
country varchar(3)
);
insert into addresses values (1, '1', '11-111 dupa jasio1','', 'UK');
insert into addresses values (2, '2', '22222 dupa jasio2','', 'UK');
insert into addresses values (3, '3', '33-333 dupa jasio3','', 'UK');
insert into addresses values (4, '4', '44444 dupa jasio4','', 'UK');
insert into addresses values (5, '5', '55555 dupa jasio5','', 'PL');
dawid=# select * from addresses ;
id | street | town | postcode | country
----+--------+--------------------+----------+---------
1 | 1 | 11-111 dupa jasio1 | | UK
2 | 2 | 22222 dupa jasio2 | | UK
3 | 3 | 33-333 dupa jasio3 | | UK
4 | 4 | 44444 dupa jasio4 | | UK
5 | 5 | 55555 dupa jasio5 | | PL
查询以更新类似于 12345 的邮政编码
update addresses set
postcode=substring(town, '^([0-9]{5})'),
town=substring(town, '^[0-9]{5}\s(.*)')
where country like '%UK%' and
(postcode is null or postcode='') and
town ~ E'^[0-9]{5} ';
查询以更新类似于 12-123 的邮政编码
update addresses set
postcode=substring(town, '^([0-9]{2}\-[0-9]{3})'),
town=substring(town, '^[0-9]{2}\-[0-9]{3}\s([\w\d\-\s]+)')
where country like '%UK%' and
(postcode is null or postcode='') and
town ~ E'^[0-9]{2}\-[0-9]{3} ';
结果
dawid=# select * from addresses ;
id | street | town | postcode | country
----+--------+-------------------+----------+---------
5 | 5 | 55555 dupa jasio5 | | PL
2 | 2 | dupa jasio2 | 22222 | UK
4 | 4 | dupa jasio4 | 44444 | UK
1 | 1 | dupa jasio1 | 11-111 | UK
3 | 3 | dupa jasio3 | 33-333 | UK