地址字符串到地址字段 VIEW 或 SELECT
Address String to Address Fields VIEW or SELECT
我有一个单行地址字段,如下所示:
Dr Robert Ruberry, West End Medical Practice, 38 Russell Street, South Brisbane 4101
我想编写一个视图,将该地址拆分为 Name、Addr1、Addr2、Suburb、Postcode 字段以用于报告目的。
我一直在尝试像这样使用 SUBSTRING 和 CHARINDEX,但它似乎没有正确拆分。
SUBSTRING([address_Field],CHARINDEX(',',[address_Field]),CHARINDEX(',',[address_Field]))
有人可以帮忙吗? TIA
这里有几个选项供您选择。如果您只是在寻找快速答案,请参阅已经回答过的类似问题:
T-SQL split string based on delimiter
如果您想更深入地了解各种选项,请查看:
http://sqlperformance.com/2012/07/t-sql-queries/split-strings
这可能符合您的要求
IF OBJECT_ID('tempdb..#test') IS NOT NULL
DROP TABLE #test
CREATE TABLE #test(id int, data varchar(100))
INSERT INTO #test VALUES (1,'Dr Robert Ruberry, West End Medical Practice, 38 Russell Street, South Brisbane 4101')
DECLARE @pivot varchar(8000)
DECLARE @select varchar(8000)
SELECT
@pivot=coalesce(@pivot+',','')+'[col'+cast(number+1 as varchar(10))+']'
FROM
master..spt_values where type='p' and
number<=(SELECT max(len(data)-len(replace(data,',',''))) FROM #test)
SELECT
@select='
select p.col1 As Name,p.col2 as Addr1,p.col3 as Addr3,p.col4 as Postcode
from (
select
id,substring(data, start+2, endPos-Start-2) as token,
''col''+cast(row_number() over(partition by id order by start) as varchar(10)) as n
from (
select
id, data, n as start, charindex('','',data,n+2) endPos
from (select number as n from master..spt_values where type=''p'') num
cross join
(
select
id, '','' + data +'','' as data
from
#test
) m
where n < len(data)-1
and substring(data,n+1,1) = '','') as data
) pvt
Pivot ( max(token)for n in ('+@pivot+'))p'
EXEC(@select)
此答案并不具体适用于 SQL,但它确实适用于 街道地址.
如果您愿意依赖 third-party,您可以将街道地址发送到 SmartyStreets International Street API 服务。
为此,您提交 HTTP GET
请求。
您的示例地址如下所示:
curl -v 'https://international-api.smartystreets.com/verify?
auth-id=YOUR+AUTH-ID+HERE&auth-token=YOUR+AUTH-TOKEN+HERE&
address1=Dr%20Robert%20Ruberry%2C%20West%20End%20
Medical%20Practice%2C%2038%20Russell%20Street%2C%20
South%20Brisbane%204101
&country=aus'
(注意地址是 url 编码的。为了便于阅读,请求被包装。)
响应将是一个 JSON
字符串,分为多个组件,然后您可以将这些组件插入数据库,但您需要:
[
{
"organization": "Dr Robert Ruberry, West End Med.",
"address1": "Dr Robert Ruberry, West End Med.",
"address2": "Russell Street",
"address3": "38 Practice",
"address4": "South Brisbane QLD 4101",
"components": {
"administrative_area": "QLD",
"building": "Russell Street",
"country_iso_3": "AUS",
"locality": "South Brisbane",
"postal_code": "4101",
"postal_code_short": "4101",
"thoroughfare": "Practice",
"thoroughfare_name": "Practice",
"sub_building_number": "38"
},
"metadata": {},
"analysis": {
"verification_status": "Partial",
"address_precision": "Locality",
"max_address_precision": "DeliveryPoint"
}
}
]
另一个好处是该服务为您提供有关地址有效性的额外信息。
(披露:我在 SmartyStreets 工作。)
我有一个单行地址字段,如下所示:
Dr Robert Ruberry, West End Medical Practice, 38 Russell Street, South Brisbane 4101
我想编写一个视图,将该地址拆分为 Name、Addr1、Addr2、Suburb、Postcode 字段以用于报告目的。
我一直在尝试像这样使用 SUBSTRING 和 CHARINDEX,但它似乎没有正确拆分。
SUBSTRING([address_Field],CHARINDEX(',',[address_Field]),CHARINDEX(',',[address_Field]))
有人可以帮忙吗? TIA
这里有几个选项供您选择。如果您只是在寻找快速答案,请参阅已经回答过的类似问题:
T-SQL split string based on delimiter
如果您想更深入地了解各种选项,请查看:
http://sqlperformance.com/2012/07/t-sql-queries/split-strings
这可能符合您的要求
IF OBJECT_ID('tempdb..#test') IS NOT NULL
DROP TABLE #test
CREATE TABLE #test(id int, data varchar(100))
INSERT INTO #test VALUES (1,'Dr Robert Ruberry, West End Medical Practice, 38 Russell Street, South Brisbane 4101')
DECLARE @pivot varchar(8000)
DECLARE @select varchar(8000)
SELECT
@pivot=coalesce(@pivot+',','')+'[col'+cast(number+1 as varchar(10))+']'
FROM
master..spt_values where type='p' and
number<=(SELECT max(len(data)-len(replace(data,',',''))) FROM #test)
SELECT
@select='
select p.col1 As Name,p.col2 as Addr1,p.col3 as Addr3,p.col4 as Postcode
from (
select
id,substring(data, start+2, endPos-Start-2) as token,
''col''+cast(row_number() over(partition by id order by start) as varchar(10)) as n
from (
select
id, data, n as start, charindex('','',data,n+2) endPos
from (select number as n from master..spt_values where type=''p'') num
cross join
(
select
id, '','' + data +'','' as data
from
#test
) m
where n < len(data)-1
and substring(data,n+1,1) = '','') as data
) pvt
Pivot ( max(token)for n in ('+@pivot+'))p'
EXEC(@select)
此答案并不具体适用于 SQL,但它确实适用于 街道地址.
如果您愿意依赖 third-party,您可以将街道地址发送到 SmartyStreets International Street API 服务。
为此,您提交 HTTP GET
请求。
您的示例地址如下所示:
curl -v 'https://international-api.smartystreets.com/verify?
auth-id=YOUR+AUTH-ID+HERE&auth-token=YOUR+AUTH-TOKEN+HERE&
address1=Dr%20Robert%20Ruberry%2C%20West%20End%20
Medical%20Practice%2C%2038%20Russell%20Street%2C%20
South%20Brisbane%204101
&country=aus'
(注意地址是 url 编码的。为了便于阅读,请求被包装。)
响应将是一个 JSON
字符串,分为多个组件,然后您可以将这些组件插入数据库,但您需要:
[
{
"organization": "Dr Robert Ruberry, West End Med.",
"address1": "Dr Robert Ruberry, West End Med.",
"address2": "Russell Street",
"address3": "38 Practice",
"address4": "South Brisbane QLD 4101",
"components": {
"administrative_area": "QLD",
"building": "Russell Street",
"country_iso_3": "AUS",
"locality": "South Brisbane",
"postal_code": "4101",
"postal_code_short": "4101",
"thoroughfare": "Practice",
"thoroughfare_name": "Practice",
"sub_building_number": "38"
},
"metadata": {},
"analysis": {
"verification_status": "Partial",
"address_precision": "Locality",
"max_address_precision": "DeliveryPoint"
}
}
]
另一个好处是该服务为您提供有关地址有效性的额外信息。
(披露:我在 SmartyStreets 工作。)