我想将地址分成两列,即街道编号和街道名称,例如 从客户那里选择地址1
前任的地址看起来像
2719 STONE CREEK DR
并将它们存储到街道编号为
2719
和街道名称为 STONE CREEK DR
。
使用
split(address1 ,'[\ ]')[0]
不仅可以分割街道。
这只是我们用来查看数据的 select 语句。
样本数据:
address1
100 HORACE GREELEY RD
55 School Street
2893 MASHIE CIR
1200 JEWEL DR
201 W RIVER RD
预期产量
+--------------+---------------------+
| streetnumber | streetname |
+--------------+---------------------+
| 100 | HORACE GREELEY RD |
| 55 | School Street |
| 2893 | MASHIE CIR |
| 1200 | JEWEL DR |
| 201 | W RIVER RD |
+--------------+---------------------+
select regexp_extract(address1,'(.*?)\\s',1) as streetnumber
,regexp_extract(address1,'\\s(.*)' ,1) as streetname
from mytable
;
+----------------+--------------------+
| streetnumber | streetname |
+----------------+--------------------+
| 100 | HORACE GREELEY RD |
| 55 | School Street |
| 2893 | MASHIE CIR |
| 1200 | JEWEL DR |
| 201 | W RIVER RD |
+----------------+--------------------+
select regexp_extract(address1,'.*?(?=\\s)',0) as streetnumber
,regexp_extract(address1,'(?<=\\s).*',0) as streetname
from mytable
;
+----------------+--------------------+
| streetnumber | streetname |
+----------------+--------------------+
| 100 | HORACE GREELEY RD |
| 55 | School Street |
| 2893 | MASHIE CIR |
| 1200 | JEWEL DR |
| 201 | W RIVER RD |
+----------------+--------------------+
select split(address1,'\\s')[0] as streetnumber
,split(address1,'^.*?\\s')[1] as streetname
from mytable
;
+----------------+--------------------+
| streetnumber | streetname |
+----------------+--------------------+
| 100 | HORACE GREELEY RD |
| 55 | School Street |
| 2893 | MASHIE CIR |
| 1200 | JEWEL DR |
| 201 | W RIVER RD |
+----------------+--------------------+