去除街道名称中的单词

时间:2018-10-16 05:54:23

标签: sql-server tsql

我有以下地址,我只需要去除实际的街道名

样本数据:

DECLARE @T AS TABLE
(
    Address varchar(100)    
)

INSERT INTO @T (Address, Desired) VALUES
('Unit 5, 10-16 Smith Street'),
('180 Long Hill Road'),
('318 Great East Build Road'),
('50 Angus Street'),
('103 Inch Street'),
('Unit 1, 28A State Road')

预期结果:

Smith Street
Long Hill Road
Great East Build Road
Angus Street
Inch Street
State Road

这怎么办?

到目前为止,我已经有了它,并且几乎在那儿,但是它以Roa和Street以及Street的形式返回Road:

SELECT  SUBSTRING(
            TRIM(
                REPLACE(
                    SUBSTRING(
                        @String, 
                        CHARINDEX(',', @String), 
                        LEN(@String)
                    ), 
                    ',', 
                '')
            ), 
            CHARINDEX(' ', 
                TRIM(
                    REPLACE(
                        SUBSTRING(
                            @String, 
                            CHARINDEX(',', @String), 
                            LEN(@String)
                        )
                    , ',', '')
                )
            ) +0, 
            205
        )

1 个答案:

答案 0 :(得分:1)

select street into #tab from (
    select 'unit 5, 10-16 Smith Street' as street
    union all select '180 Long Hill Street'
    union all select '103 Inch Street') t

select
    reverse(rev) street,
    reverse(substring(rev,1,PATINDEX('%[^0-9][0-9]%',rev)))
from
    (select reverse(street) as rev from #tab) t

drop table #tab