字符串内容是:23*******。数字的位数是不定的,*代表非数字字符。我想把数字取出来。使用截取函数时,必须知道什么位置非数字字符出现。请高手指教。

解决方案 »

  1.   

    declare @s varchar(10)
    set @s='23*******'
    select left(@s,patindex('%[^0-9]%',@s)-1)
    /*
    ----------
    23(1 個資料列受到影響)*/
      

  2.   

    declare @s varchar(30)
    set @s='11654asdfs刘德华'
    select left(@s,patindex('%[^0-9]%',@s)-1)/*
                                   
    ------------------------------ 
    11654(所影响的行数为 1 行)*/
      

  3.   


    --建立如下函数(非0-9)
    go
    create function getnewstr(@oldstr varchar(100)) returns varchar(100)
    as
      begin
        declare @i int
        set @i = 1
        while @i <= len(@oldstr)
        if substring(@oldstr, @i, 1) like('[^0-9]')
           set @oldstr = replace(@oldstr, substring(@oldstr, @i, 1), '')
        else
           set @i = @i +1
        return @oldstr
    end
    goselect col = dbo.getnewstr(col) from tb where area like('%[^0-9]%')
      

  4.   

    select left(col,patindex('%[^0-9]%',col)-1)就OK了