sqlserver 将 “用 特定字符 分隔的一个字段” 拆分成多个字段,然后两个表之间数据更新

将源TXT文件sourceFile_table.txt导入数据库,生成新表dbo.sourceFile_table。新增字段lon、lat、shi、xian

源表dbo.sourceFile_table

源表dbo.GeographyInfo

SQL语句:

--删除表dbo.sourceFile_table中 双隐号
UPDATE  sourceFile_table
SET     [s_id] = REPLACE([s_id],'"','') ,
        [s_lon_lat] = REPLACE([s_lon_lat],'"','') ,
        [s_shi_xian] = REPLACE([s_shi_xian],'"','')
SELECT * FROM sourceFile_table
--查询表dbo.sourceFile_table:将逗号分隔的一个字段拆分成多个字段 ;将空格分隔的一个字段拆分成多个字段
SELECT TOP 1000 [s_id],
                [s_lon_lat],  
                [s_shi_xian],
                substring([s_lon_lat],1,charindex(',',[s_lon_lat])) lon,
                substring([s_lon_lat],charindex(',',[s_lon_lat]) +1,30) lat,          
                substring(s_shi_xian,1,charindex(' ',s_shi_xian)) shi,
                substring(s_shi_xian,charindex(' ',s_shi_xian) +1,30) xian
from sourceFile_table
--更新表dbo.sourceFile_table:将逗号分隔的一个字段拆分成多个字段 ;将空格分隔的一个字段拆分成多个字段
UPDATE  sourceFile_table
SET     lon=substring([s_lon_lat],1,charindex(',',[s_lon_lat])),
        lat=substring([s_lon_lat],charindex(',',[s_lon_lat]) +1,30),  
        shi=substring([s_shi_xian],1,charindex(' ',[s_shi_xian])),
        xian=substring([s_shi_xian],charindex(' ',[s_shi_xian]) +1,30)        
SELECT * FROM dbo.sourceFile_table
 
--更新表dbo.sourceFile_table:将拆分后, 字段lon数据中 逗号 删除,字段shi数据中 空格 删除
UPDATE  sourceFile_table
SET     [lon] = REPLACE([lon],',',''),
        [shi] = REPLACE([shi],' ','')
SELECT * FROM dbo.sourceFile_table
 
--更新表dbo.GeographyInfo:两个表之间数据更新,更新表dbo.GeographyInfo中字段shi、xian、lon、lat数据
update GeographyInfo
set GeographyInfo.shi=TS.shi,
    GeographyInfo.xian=TS.xian,
    GeographyInfo.lon=TS.lon,
    GeographyInfo.lat=TS.lat
from GeographyInfo,sourceFile_table TS
where GeographyInfo.rerid=TS.s_id
 
--查询dbo.GeographyInfo:表更新后的数据,最新1000条数据,根据id降序排序
SELECT TOP 1000 [id],
                [rerid],
                [shi],
                [xian],
                [lon],
                [lat]
FROM [dbo].[GeographyInfo]
order by id desc

--删除表dbo.sourceFile_table数据
delete from sourceFile_table

执行结果:

sqlserver》单击数据库》新建查询(N)》复制SQL语句到空白处》 !执行(X)

。。。

-----------------------------------------------------------------------简单示例1-----------------------------------------------------------------------

SQL语句1:

--新建表test
create table test(pp varchar(30))  
go
select * from test

--新增数据
insert into test values('耐克 DS001'),('安踏 AT002'),('阿迪达斯 AD009')  
go
select * from test

--查询表test:将空格分隔的一个字段拆分成多个字段
select 
	substring(pp,1,charindex(' ',pp))pp1, 
	substring(pp,charindex(' ',pp) +1,30) pp2
from test 
go

--删除表test
drop table test 
go

执行结果:

sqlserver》单击数据库》新建查询(N)》复制SQL语句到空白处》 !执行(X)

SQL语句2:

--
SELECT  LEFT(商品名称, CHARINDEX('  ', 商品名称 + '  ') - 1) AS 品牌 ,
        STUFF(商品名称, 1, CHARINDEX('  ', 商品名称 + '  ') + 1, '') AS 商品代码
FROM    ( VALUES ( '耐克  DS001'), ( '安踏  AT002'), ( '阿迪达斯  AD009') ) t ( 商品名称 ); 

--
select '耐克  DS001' as col1 into #Idontkonwthis
select left(col1,(select charindex('  ',col1))), substring(col1,(select charindex('  ',col1)),(select len(col1))) from #Idontkonwthis

执行结果:

-----------------------------------------------------------------------简单示例2-----------------------------------------------------------------------

SQL语句:

--①横向
declare @str1 varchar(max)
set @str1='福尔摩斯,tellme,他,在哪里'
set @str1=REPLACE(@str1,',',''',''')
exec ('select '''+@str1+'''')
 
--②竖向
declare @str2 varchar(max)
set @str2='福尔摩斯,tellme,他,在哪里'
set @str2='select '''+replace(@str2,',',''' as col union all select ''')
--print @str
exec(@str2+'''')

--xml解法
declare @a nvarchar(max)
declare @xml xml
set @a='aa;bb;cc;dd' 
set @xml=cast('<root><col val="'+replace(@a,';','" /><col val="')+'"></col></root>' as XML)
-- select @xml
select n=t.c.value('@val','varchar(255)') from @xml.nodes('/root/col') t(c)

执行结果:

sqlserver》单击数据库》新建查询(N)》复制SQL语句到空白处》 !执行(X)

猜你喜欢

转载自blog.csdn.net/LaOngDaoxing/article/details/82665661