2016-06-15 59 views
0

如何從保存在我的col1col2中的長地址中提取短地址?問題是我想要的名字爲我的長地址的地區和城市如何從SQL Server的col1中保存的長地址中提取短地址

例子:

District ALBERT numero 1234 city CASABLANCA région de NORTH country MOROOCO . 

我要爲我剛纔的短地址:

District ALBERT city CASABLANCA 

請我需要幫助,我在我的col1有很多註冊,我不能手動做!

對不起,我英文不好

+1

以相同的模式始終是地址? 'District numero city ....' –

+0

是Jorge。區和市 – aziz

回答

2

測試可以使用:

update yourTable 
    set col2 = substring(col1,1,(charindex(col1,'numero')-1) 
       + substring(col1, 
          (charindex(col1,'city'), 
          ((charindex(col1,'région')-1)-charindex(col1,'city'))) 
+0

非常感謝Jorge爲我工作。謝謝 – aziz

+0

嗨,阿齊茲,很高興我能幫忙,請考慮接受它。點擊此答案左側的V圖標。 –

0

在SQL Server 2008

declare @longAddress varchar(max) 
set @longAddress = 'District ALBERT numero 1234 city CASABLANCA région de NORTH country MOROOCO ' 

--assuming regin follow city 
select CHARINDEX('numero',@longAddress)-- this is how you get position of numero and region 


--do a substring 
select SUBSTRING(@longAddress,1,CHARINDEX('numero',@longAddress)-1) -- get the district 
select SUBSTRING(@longAddress,charindex('city',@longAddress),charindex('région',@longAddress)-charindex('city',@longAddress)) -- get the city 

--combine 
select SUBSTRING(@longAddress,1,CHARINDEX('numero',@longAddress)-1) -- get the district 
+SUBSTRING(@longAddress,charindex('city',@longAddress),charindex('région',@longAddress)-charindex('city',@longAddress)) -- get the city 
+0

抱歉,沒有看到結果。我會修改 – chungtinhlakho

+0

非常感謝你chungtinhlakho其工作完美的解決方案謝謝你。 – aziz