我有一字符串,该字符串的组成是这样的:年份+类别+号码。年份这一部分有可能是两位(如09),也有可能是4位(2009)。
举两个年份位数不同的字符串:(1)200913313001011(年份为4位,第5位表示类别)(2)0923313001012(年份为2位,第3位表示类别)。
求一个根据该字符串获取获取其类别的函数,请指教!

解决方案 »

  1.   


    create table #AA
    (
    sss varchar(30)
    )
    insert into #AA
    select '200913313001011' union all
    select '0923313001012'select (case when substring(sss,1,1)='2' then substring(sss,5,4) else substring(sss,3,4) end) as leixing from #AA
    -------------------------
    leixing
    1331
    2331
      

  2.   

    select left(col , len(col) - 11) from tb
      

  3.   


    create table #AA
    (
    sss varchar(30)
    )
    insert into #AA
    select '200913313001011' union all
    select '0923313001012'select (case when substring(sss,1,1)='2' then substring(sss,5,1) else substring(sss,3,1) end) as leixing from #AA
    leixing
    -------
    1
    2(2 行受影响)
      

  4.   

    --如果后面都是11位.create table tb(col varchar(15))
    insert into tb values('200913313001011')
    insert into tb values('0923313001012')
    goselect left(col , len(col) - 11) 年,
           left(right(col,11),1) 类别
    from tbdrop table tb/*
    年               类别   
    --------------- ---- 
    2009            1
    09              2(所影响的行数为 2 行)
    */
      

  5.   

    号码的长度是固定的话
    你给的数据里
    年份+类别+号码
    号码的长度是 10select right(left(fieldvalue,len(fieldvalue)-10),1) as '类别' from tb
      

  6.   

    declare @t table(col varchar(20))
    insert @t select '200913313001011' 
    union all select '0923313001012'select col,(case when len(col)=15 then substring(col,5,1) else substring(col,3,1) end) type from @tcol                  type
    -------------------- ----
    200913313001011      1
    0923313001012        2(2 行受影响)
      

  7.   

    select left(right([Field],11),1) from [Table]
      

  8.   

    declare @s nvarchar(20)
    set @s='200913313001011'
    --set @s='0923313001012'
    select substring(reverse(@s),11,1)
    /*
    1
    */
      

  9.   

    if object_id('[tb]') is not null drop table [tb]
    go
    create table [tb]([编号] varchar(14))
    insert [tb]
    select '86300493' union all
    select '92207560' union all
    select '91226410' union all
    select '00217636.X' union all
    select '99336935.9' union all
    select '00305178.1' union all
    select '01328822.9' union all
    select '200330101225.9' union all
    select '03224068.6'--非严格方法:
    select [编号],[对应年份]=left([完整编号],4),[对应类型]=substring([完整编号],5,1)
    from
    (
    select  [编号],[完整编号]=case when left([编号],2) in ('19','20') then [编号]
    when left([编号],1)='0' then '20'+[编号]
    else '19'+[编号]
    end
    from tb
    ) t
    /*
    编号             对应年份     对应类型
    -------------- -------- ----
    86300493       1986     3
    92207560       1992     2
    91226410       1991     2
    00217636.X     2000     2
    99336935.9     1999     3
    00305178.1     2000     3
    01328822.9     2001     3
    200330101225.9 2003     3
    03224068.6     2003     2(9 行受影响)*/