A表
type  字段
08-04-aa-bb
每个字段有3个-,固定的
我想拆分他变成4个字段,字段名称无所谓,变成n1,n2,n3,n4也行

解决方案 »

  1.   


    create  function[dbo].[stringsplit](@strnvarchar(max),@spliternvarchar(10))returns@tbtable(chnvarchar(256))asbegindeclare@numint,@posint,@nextposintset@num=0set@pos=1while(@pos<=len(@str))beginselect@nextpos=charindex(@spliter,@str,@pos)if(@nextpos=0or@nextposisnull)select@nextpos=len(@str)+1insertinto@tbvalues(rtrim(ltrim(substring(@str,@pos,@nextpos-@pos))))select@pos=@nextpos+1endreturnendSELECT * from  [dbo].[StringSplit] ('08-04-aa-bb' ,'-')
      

  2.   

    写的太复杂了,08
    04
    aa
    bb我是希望形成一个字段select * ,'' as n1,'',as n2,'' as n3,'' as n4 from 
      

  3.   


    with tb(a) as(
    select '08-04-aa-bb'),
    tbb (a)as (
    select RIGHT(a,len(a)-CHARINDEX('-',a))from tb
    ),
    tbbb(a)as(
    select RIGHT(a,len(a)-CHARINDEX('-',a))from tbb
    ),
    tbbbb(a)as(
    select RIGHT(a,len(a)-CHARINDEX('-',a))from tbbb
    )
    select LEFT(a,charindex('-',A)-1) from tb union all
    select LEFT(a,charindex('-',A)-1) from tbb union all
    select LEFT(a,charindex('-',A)-1) from tbbb union all
    select * from tbbbb
      

  4.   

    我不是要数据集,我要字段呀select * ,'' as n1,'',as n2,'' as n3,'' as n4 from 
    也就是如何得到第一个-前面的,第二个-前面的
      

  5.   


    with tb(a) as( select '08-04-aa-bb'), 
    tbb (a)as ( select RIGHT(a,len(a)-CHARINDEX('-',a))from tb ), 
    tbbb(a)as( select RIGHT(a,len(a)-CHARINDEX('-',a))from tbb ), 
    tbbbb(a)as( select RIGHT(a,len(a)-CHARINDEX('-',a))from tbbb ) 
    select LEFT(tb.a,charindex('-',tb.A)-1)n1,n2,n3,tbbbb.a n4 from tb left join 
    (select LEFT(tbb.a,charindex('-',tbb.A)-1)n2 from tbb)b on 1=1 left join 
    (select LEFT(tbbb.a,charindex('-',tbbb.A)-1)n3 from tbbb)c on 1=1 left join
     tbbbb on 1=1这样?