假如有一表tableA,有字段name
name里的值是类似这样的数据:
'按时的撒旦123元(其中含存款11元)'
'一类似的预存9600元(新势力)'内容没有规律,唯一可确定的是字符串里有钱数,比如上面的例子取出来的结果是:123和9600
怎么把钱数取出来?
求plsql, oracle是9I不支持正则表达.

解决方案 »

  1.   

    这个 可能么? 我也想看看这样的SQL 
      

  2.   

    如果‘元’前面只有1个数字的,可以这样
    SQL> with tmp as
      2  (select '按时的撒旦123元(其中含存款11元)' name from dual
      3  union all
      4  select '一类似的预存9600元(新势力)' from dual
      5  )
      6  select name,trim(translate(substr(name,1,instr(name,'元')),translate(substr(name,1,instr(name,'元')),'0123456789',' '),' '))  num from tmp;
     
    NAME                                   NUM
    ------------------------------------------- --------------------------------------------------------------------------------
    按时的撒旦123元(其中含存款11元) 123
    一类似的预存9600元(新势力) 9600
     
      

  3.   

    dingjun123的只能解决‘元’前面只有1个数字的,有的前面是有别的数字的
    比如:WCDMA(3G)-3G按时的30000元(其中按时的存款10632元 
    出来的结果是 3330000
    不是要的 结果:30000
      

  4.   

    应该能用SQL搞定,用PL/SQL那就太简单了
      

  5.   

    我写了个,这东西还真不好写,完全用函数,with等拼凑的,效率别指望多好了DINGJUN123>with tmp as
      2    (select '按时1的撒旦123元(其中含存款11元)' name from dual
      3      union all
      4     select '一类似的预存9600元(新势力)' from dual
      5     union all
      6     select  'WCDMA(3G)-3G按时的30000元(其中按时的存款10632元)' from dual
      7     )
      8     ,t as
      9     (select  a,b,name
     10       from (
     11                select rownum a,name,length(substr(name,1,instr(name,'元')-1)) b
     12                 from tmp
     13               )
     14     )
     15     ,m as
     16    ( select a,b,name,substr(name,lv,1) x
     17       ,translate(substr(name,lv,1),nvl(translate(substr(name,lv,1),'0123456789',' '),0),' ') c
     18       ,lv
     19     from
     20         (
     21             select a,b,name, lv from t,
     22             (select level lv from dual
     23                connect by level<=(select max(b)
     24                from t)
     25              )
     26             where lv<=b
     27          )
     28     )
     29    ,rs as
     30     (
     31       select  *  from m
     32        where m.lv >
     33          (select max(lv)
     34            from m mx
     35            where mx.c=' ' and mx.a=m.a
     36       )
     37     )
     38  select a,name,
     39           to_number(replace(max(sys_connect_by_path(c,',')),',')) num
     40  from
     41    (
     42    select a,b,x,c,name,lv,
     43   row_number() over(partition by a order by lv) rn
     44     from rs
     45     )
     46   start with rn =1
     47   connect by prior rn = rn-1 and prior a=a
     48   group by a,name
     49   order by a;         A NAME                                                      NUM
    ---------- -------------------------------------------------- ----------
             1 按时1的撒旦123元(其中含存款11元)                          123
             2 一类似的预存9600元(新势力)                             9600
             3 WCDMA(3G)-3G按时的30000元(其中按时的存款10632元)        30000