有一个数据表如下:
求一个select语句,结果如下(从CouponDenominatoin字段中找到两个数字分别放到两个新增字段):

解决方案 »

  1.   


    create table test(id int, name varchar(30), f1 int, f2 int)
    go
    insert into test(id, name) values(1,'满100元减30元')
    insert into test(id, name) values(1,'满59元减19元')
    insert into test(id, name) values(1,'满10元减3元')
    go
    update test 
    set f1= substring(name,2,charindex('元减',name) -2), 
        f2=replace(substring(name,charindex('元减',name)+2,len(name)),'元','')
    go
    select * from test 
    go
    drop table test 
    goid          name                           f1          f2
    ----------- ------------------------------ ----------- -----------
    1           满100元减30元                      100         30
    1           满59元减19元                       59          19
    1           满10元减3元                        10          3(3 行受影响)
      

  2.   

    格式固定可这样用
    e.g.
    SELECT 
    *
    ,fieid1=SUBSTRING(CouponDenomination,2,CHARINDEX('元',CouponDenomination)-2)
    ,fieid2=SUBSTRING(CouponDenomination,CHARINDEX('减',CouponDenomination)+1,LEN(CouponDenomination)-(CHARINDEX('减',CouponDenomination)+1))
    FROM table1
      

  3.   

    REGEXP_SUBSTRSELECT  REGEXP_SUBSTR('hello world','\d{1,}') INTO  field1
       FROM  YourTable;