sqlserver 將 “用 特定字符 分隔的一個字段” 拆分成多個字段,然后兩個表之間數據更新


將源TXT文件sourceFile_table.txt導入數據庫,生成新表dbo.sourceFile_table。新增字段lon、lat、shi、xian

源表dbo.sourceFile_table


源表dbo.GeographyInfo

SQL語句:

 1 --刪除表dbo.sourceFile_table中 雙隱號
 2 UPDATE  sourceFile_table
 3 SET     [s_id] = REPLACE([s_id],'"','') ,
 4         [s_lon_lat] = REPLACE([s_lon_lat],'"','') ,
 5         [s_shi_xian] = REPLACE([s_shi_xian],'"','')
 6 SELECT * FROM sourceFile_table
 7 --查詢表dbo.sourceFile_table:將逗號分隔的一個字段拆分成多個字段 ;將空格分隔的一個字段拆分成多個字段
 8 SELECT TOP 1000 [s_id],
 9                 [s_lon_lat],  
10                 [s_shi_xian],
11                 substring([s_lon_lat],1,charindex(',',[s_lon_lat])) lon,
12                 substring([s_lon_lat],charindex(',',[s_lon_lat]) +1,30) lat,          
13                 substring(s_shi_xian,1,charindex(' ',s_shi_xian)) shi,
14                 substring(s_shi_xian,charindex(' ',s_shi_xian) +1,30) xian
15 from sourceFile_table
16 --更新表dbo.sourceFile_table:將逗號分隔的一個字段拆分成多個字段 ;將空格分隔的一個字段拆分成多個字段
17 UPDATE  sourceFile_table
18 SET     lon=substring([s_lon_lat],1,charindex(',',[s_lon_lat])),
19         lat=substring([s_lon_lat],charindex(',',[s_lon_lat]) +1,30),  
20         shi=substring([s_shi_xian],1,charindex(' ',[s_shi_xian])),
21         xian=substring([s_shi_xian],charindex(' ',[s_shi_xian]) +1,30)        
22 SELECT * FROM dbo.sourceFile_table
23  
24 --更新表dbo.sourceFile_table:將拆分后, 字段lon數據中 逗號 刪除,字段shi數據中 空格 刪除
25 UPDATE  sourceFile_table
26 SET     [lon] = REPLACE([lon],',',''),
27         [shi] = REPLACE([shi],' ','')
28 SELECT * FROM dbo.sourceFile_table
29  
30 --更新表dbo.GeographyInfo:兩個表之間數據更新,更新表dbo.GeographyInfo中字段shi、xian、lon、lat數據
31 update GeographyInfo
32 set GeographyInfo.shi=TS.shi,
33     GeographyInfo.xian=TS.xian,
34     GeographyInfo.lon=TS.lon,
35     GeographyInfo.lat=TS.lat
36 from GeographyInfo,sourceFile_table TS
37 where GeographyInfo.rerid=TS.s_id
38  
39 --查詢dbo.GeographyInfo:表更新后的數據,最新1000條數據,根據id降序排序
40 SELECT TOP 1000 [id],
41                 [rerid],
42                 [shi],
43                 [xian],
44                 [lon],
45                 [lat]
46 FROM [dbo].[GeographyInfo]
47 order by id desc
48 
49 --刪除表dbo.sourceFile_table數據
50 delete from sourceFile_table

執行結果:

sqlserver》單擊數據庫》新建查詢(N)》復制SQL語句到空白處》 !執行(X)

。。。

-----------------------------------------------------------------------簡單示例1-----------------------------------------------------------------------

SQL語句1:

 1 --新建表test
 2 create table test(pp varchar(30))  
 3 go
 4 select * from test
 5 
 6 --新增數據
 7 insert into test values('耐克 DS001'),('安踏 AT002'),('阿迪達斯 AD009')  
 8 go
 9 select * from test
10 
11 --查詢表test:將空格分隔的一個字段拆分成多個字段
12 select 
13     substring(pp,1,charindex(' ',pp))pp1, 
14     substring(pp,charindex(' ',pp) +1,30) pp2
15 from test 
16 go
17 
18 --刪除表test
19 drop table test 
20 go

執行結果:

sqlserver》單擊數據庫》新建查詢(N)》復制SQL語句到空白處》 !執行(X)


SQL語句2:

1 --
2 SELECT  LEFT(商品名稱, CHARINDEX('  ', 商品名稱 + '  ') - 1) AS 品牌 ,
3         STUFF(商品名稱, 1, CHARINDEX('  ', 商品名稱 + '  ') + 1, '') AS 商品代碼
4 FROM    ( VALUES ( '耐克  DS001'), ( '安踏  AT002'), ( '阿迪達斯  AD009') ) t ( 商品名稱 ); 
5 
6 --
7 select '耐克  DS001' as col1 into #Idontkonwthis
8 select left(col1,(select charindex('  ',col1))), substring(col1,(select charindex('  ',col1)),(select len(col1))) from #Idontkonwthis

執行結果:

-----------------------------------------------------------------------簡單示例2-----------------------------------------------------------------------

SQL語句:

 1 --①橫向
 2 declare @str1 varchar(max)
 3 set @str1='福爾摩斯,tellme,他,在哪里'
 4 set @str1=REPLACE(@str1,',',''',''')
 5 exec ('select '''+@str1+'''')
 6  
 7 --②豎向
 8 declare @str2 varchar(max)
 9 set @str2='福爾摩斯,tellme,他,在哪里'
10 set @str2='select '''+replace(@str2,',',''' as col union all select ''')
11 --print @str
12 exec(@str2+'''')
13 
14 --xml解法
15 declare @a nvarchar(max)
16 declare @xml xml
17 set @a='aa;bb;cc;dd' 
18 set @xml=cast('<root><col val="'+replace(@a,';','" /><col val="')+'"></col></root>' as XML)
19 -- select @xml
20 select n=t.c.value('@val','varchar(255)') from @xml.nodes('/root/col') t(c)

執行結果:

sqlserver》單擊數據庫》新建查詢(N)》復制SQL語句到空白處》 !執行(X)

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM