数据库里有一个字段的值为:  ,2,3,4,
这里的位数不确定即也可以是,,23,2,24,
我怎么去取这个4 或者 24

解决方案 »

  1.   

    假如该字段被命名为:uname
    则要得到字符e的话:
    select substring(uname,len(uname),1) from 表名where 条件-----------------------
    给分哈,要
      

  2.   

    create function get_str(@s varchar(100))
    returns varchar(10)
    as
    begin
      declare @return_s varchar(10)
      declare @i int
      declare @dou1 int
      declare @dou2 int
      declare @bz char(1)
      select @i=1,@dou1=0,@dou2=0,@bz='1'
      while @i<=len(@s)
      begin
        if substring(@s,@i,1)=','
        begin
          if @bz='1'
            select @dou1=@i,@bz='2'
          else
            select @dou2=@i,@bz='1'
        end
        select @i=@i+1
      end
      if @dou1<@dou2
        select @return_s=substring(@s,@dou1+1,@dou2-@dou1-1)
      else
        select @return_s=substring(@s,@dou2+1,@dou1-@dou2-1)
      return @return_s
    end
    goselect dbo.get_str(field) from table
      

  3.   

    declare @a varchar(100)
    set @a=',sfh,jt,yr,t,e,rt,hhhhh,'
    create table tb(a varchar(100))
    insert into tb select ',sfh,jt,yr,t,e,rt,hhhhh,'
    insert into tb select ',sfh,jt,yr,t,e,rt,h,hhh,h,'
    insert into tb select ',sfh,jt,yr,t,e,rt,h,hhhh,,'
    select substring(reverse(a),2,charindex(',',reverse(a),2)-2) from tb
    go
    drop table tb
      

  4.   

    ---------------------------------------------------------------------------------------------------- 
    hhhhh
    h
    (所影响的行数为 3 行)
      

  5.   

    楼上正解
    再在结果上加个reverse,就完整了