日期:2014-05-18  浏览次数:20832 次

存储过程中很复杂的字符串截取,弄了几天,我头大了
有如下字符串:(下列字符间相连,没有空格,为了方便观看,手工弄的空格)

离线记录^songlijun|8888888888|3500320083208#3500600520890#3500741122924#3500790143280#3501010510869#3500741129750

%离线记录^xieyuan|0540002070|3500320083208#3500600520890#3500741122924#3500790143280#3501010510869#3500741129750 $5480011708|3500320083208#3500600520890#3500741122924#3500790143280#3501010510869#3500741129750
  $5480011701|3500320083208#3500600520890#3500741122924#3500790143280#3501010510869#3500741129750
  $0540002070|3500320083208#3500600520890#3500741122924#3500790143280#3501010510869#3500741129750

%离线记录^xieyuan|0540002070|3500320083208#3500600520890#3500741122924#3500790143280#3501010510869#3500741129750
  $0540002070|3500320083208#3500600520890#3500741122924#3500790143280#3501010510869#3500741129750

解释:
上面是多个文件中的数据拼接起来的。
以“%”相隔的是不同文件中的数据,上面只有3个文件,故只有2个“%”,若有n个文件,那么会有(n-1)个“%”。
以“|”相隔的依次为物流员(如“离线记录^songlijun”,物流员为songlijun),站点编号,包编号集合(如“3500320083208#3500600520890#3500741122924#3500790143280#3501010510869#3500741129750”)
以“^”相隔的“离线记录”为固定内容,“^”后为物流员(songlijun)
以“#”相隔的为包编号(如“3500320083208#3500600520890#3500741122924#3500790143280#3501010510869#3500741129750”中共有6个包编号,形如“3500320083208”)

目的:
得到物流员,站点编号,包编号(如“3500320083208”),插入到表中。

遇到问题:
做了几天,if-else太多了,弄得几个就晕了。

希望有好心人帮忙!感激不尽啊!第一次发帖,不知道给多少分。分不够的话,还可以再加啊! 要联系我的话,可以加我QQ:872082002

------解决方案--------------------
SQL code

/*
create function [dbo].[m_split2](@c varchar(max),@split varchar(2))   
  returns @t table(col varchar(max))   
  as   
    begin   
      while(charindex(@split,@c)<>0)   
        begin   
            if(substring(@c,1,charindex(@split,@c)-1)!=' ')
            begin
          insert @t(col) values (substring(@c,1,charindex(@split,@c)-1))   
            end
             set @c = stuff(@c,1,charindex(@split,@c),'')    
        end   
        if(@c!=' ')
        begin
      insert @t(col) values (@c) 
      end  
      return   
end
*/
declare @T nvarchar(max)
set @T=
'离线记录^songlijun|8888888888|3500320083208#3500600520890#3500741122924#3500790143280#
3501010510869#3500741129750%离线记录^xieyuan|0540002070|3500320083208#3500600520890#350
0741122924#3500790143280#3501010510869#3500741129750$5480011708|3500320083208#350060052
0890#3500741122924#3500790143280#3501010510869#3500741129750$5480011701|3500320083208#3
500600520890#3500741122924#3500790143280#3501010510869#3500741129750$0540002070|3500320
083208#3500600520890#3500741122924#3500790143280#3501010510869#3500741129750%离线记录^x
ieyuan|0540002070|3500320083208#3500600520890#3500741122924#3500790143280#3501010510869
#3500741129750$0540002070|3500320083208#3500600520890#3500741122924#3500790143280#35010
10510869#3500741129750'

select * into #t from [dbo].[m_split2](@T,'%')

update #t set col=replace(col,' ','')
update #t set col=replace(col,char(13),'')
update #t set col=replace(col,char(10),'')

select 
substring(col,charindex('^',col)+1,
charindex('|',col)-charindex('^',col)-1) as id,
right(col,len(col)-charindex('|',col)) as [value] into #t2
from #t
drop table #t

select id, left(value,charindex('|',value)-1) as nid,
right(value,len(value)-charindex('|',value)+1) as value into #t3 from (
select A.id, B.value 
from( 
    select id, [value] = 
convert(xml,' <root> <v>' + replace([value], '$', ' </v> <v>') + ' </v> </root>') from #t2 
)A 
outer apply( 
    select value = N.v.value('.', 'varchar(100)') from A.[value].nodes('/root/v') N(v) 
)B 
) aa

drop table #t2

select A.id as 物流员, A.nid as 站点编号,B.value as 编号集合 
from( 
    select id,nid, [value] = 
convert(xml,' <root> <v>' + replace([value], '#', ' </v> <v>') + ' </v> </root>') from #t3 
)A 
outer apply( 
    select value = N.v.value('.', 'varchar(100)') from A.[value].nodes('/root/v')