如何把字段里的数字查出来例如
col1
我心飞扬2011号
网名A2012
网名B2012
结果
col1
2011
2012
2012

解决方案 »

  1.   


    --没正则的日子太痛苦,呵呵
    with t(col1) as(
    select '我心飞扬2011号' union select '网名A2012' union select '网名B2012'
    )
    select col1,left(substring(col1,patindex('%[0-9]%',col1),len(col1))
    ,patindex('%[^0-9]%',substring(col1,patindex('%[0-9]%',col1),len(col1))+'a')-1) result from t
    order by result;
    /*
    col1           result
    -------------- --------------
    我心飞扬2011号      2011
    网名A2012        2012
    网名B2012        2012(3 行受影响)
    */
      

  2.   

    select col1,left(substring(col1,patindex('%[0-9]%',col1),len(col1))
        ,patindex('%[^0-9]%',substring(col1,patindex('%[0-9]%',col1),len(col1))+'a')-1) result from t
        order by result;
    /
    新手,这个用的是通配符嘛???
      

  3.   


    declare @s varchar(20)
    set @s='我心飞扬2011号'
    --set @s='网名A2012' select substring(@s,patindex('%[0-9]%',@s),4)
      

  4.   

    declare @s table(col varchar(20))
    insert into @s
    select '我心飞扬2011号' union all
    select '网名A2012' union all
    select '网名B2012'select substring(col,patindex('%[0-9]%',col),4) as col from @s-----
    /*
    col
    2011
    2012
    2012
    */
      

  5.   

    declare @s varchar(20)
    set @s='我心飞扬2011号'
    --set @s='网名A2012' select substring(@s,patindex('%[0-9]%',@s),4)