如何使用 SQL 服务器从保存在我的 col1 中的长地址中提取短地址

How can I extract a short address from a long address saved in my col1 with SQL Server

如何从保存在 col1 中的长地址中提取短地址到 col2?问题是我想要地区和城市的名称

我的长地址示例:

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

我只想要我的简短地址:

District ALBERT city CASABLANCA 

我需要帮助,我的 col1 中有很多注册,我无法手动完成!

抱歉我的英语不好

在 sql 服务器 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

您可以使用:

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