• 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)

  • 相关阅读:
    Redis教程_2
    Redis教程_1
    机器学习概念_2
    机器学习概念_1
    [极客大挑战 2019]LoveSQL
    [极客大挑战 2019]EasySQL
    [SUCTF 2019]EasySQL
    [强网杯 2019]随便注
    [HCTF 2018] WarmUp
    php代码函数笔记
  • 原文地址:https://www.cnblogs.com/yangchengdejishuzhai/p/9644878.html
Copyright © 2020-2023  润新知