在数据库中表test,其中有一个string类型字段,如:context
"if a>b then a=100;"需要用SQL把string类型中得100这个数字取出来。例如:
select substr(context,-2)from test最后结果要为:
substr(context,-1)
100

解决方案 »

  1.   

    得自己编写一个函数,根据每个字符对应的ASCII值来判断是否抽取这个数。
    不知道ORACLE自己有没有这个函数。
      

  2.   

    是的,我也是这样想的,编写一个function,来实现。哪位老大知道怎么弄啊?
      

  3.   

    look this:ops$tkyte@ORA9IR2> create or replace package demo_pkg
      2  as
      3          g_bad_chars varchar2(255);
      4  end;
      5  /
     
    Package created.
     
    ops$tkyte@ORA9IR2> create or replace package body demo_pkg
      2  as
      3  begin
      4          for i in 1 .. 255
      5          loop
      6                  if ( chr(i) not between 'a' and 'z' )
      7                     and
      8                     ( chr(i) not between 'A' and 'Z' )
      9                     and
     10                     ( chr(i) not between '0' and '9' )
     11                     and
     12                     ( chr(i) not in ( '-', '_' ) )
     13                  then
     14                          g_bad_chars := g_bad_chars || chr(i);
     15                  end if;
     16          end loop;
     17  end;
     18  /
     
    Package body created.
     
    ops$tkyte@ORA9IR2>
    ops$tkyte@ORA9IR2>
    ops$tkyte@ORA9IR2> drop table t;
     
    Table dropped.
     
    ops$tkyte@ORA9IR2>
    ops$tkyte@ORA9IR2> create table t ( str varchar2(30) );
     
    Table created.
     
    ops$tkyte@ORA9IR2> insert into t values ( '5%@%52345-fafdA_5@$#' );
     
    1 row created.
     
    ops$tkyte@ORA9IR2>
    ops$tkyte@ORA9IR2> begin
      2          for x in ( select translate( str, 'a'||demo_pkg.g_bad_chars, 'a' ) 
    str from t )
      3          loop
      4                  dbms_output.put_line( x.str );
      5          end loop;
      6  end;
      7  /
    552345-fafdA_5
     
    PL/SQL procedure successfully completed.
      

  4.   

    cenlmmx(学海无涯苦作舟)你好,感谢你的回复。
    实际情况我需要用sql把数据查询出来,表中有很多数据,而且查询也有其他字段,只是其中有个string字段时这样的:
    context
    "if a>b and a=100 then a=100;"
    "if a>b and a>1 then a=1;"
    "if a>b or a<1 then a=33;"
    "if a>b then a=133;"
    "if a>b then a=130;"最后我想得到的结果是:最后一个数字,即最后一个分号和等号间的数字
    context
    100
    1
    33
    133
    130
      

  5.   

    oracle 10g可以用REGEXP_INSTR 直接定位第一位和最后一位,如果你只有1个数字的话。
    然后用substr就可以直接出来了。select substr(context,
    REGEXP_INSTR(context,'[[:digit:]]'), 
    REGEXP_INSTR(context,'[[:digit:]]+',1,1) - REGEXP_INSTR(context,'[[:digit:]]')
    )from test
      

  6.   

    写个PL/SQL函数吧~
    否则这sql一点可读性都没..
      

  7.   

    哦,不好意思,回帖的时候你先回了,照你上面说的,用10g的特性匹配就更方便了。
    参考:
    http://www.oracle.com/technology/global/cn/oramag/webcolumns/2003/techarticles/rischert_regexp_pt1.html
      

  8.   

    for i in 1 .. 255
    ==>
    for i in 255 .. 1
    再加上一些控制代码
    如果10g,最好象楼上所说的用正则表达式
      

  9.   

    select replace(regexp_substr(replace(context,' ',''),'[[:digit:]]*;$'),';','') from test;最外层的replace将分号去掉,最内层的replace是防止最后的分号和数字有空格,用了replace不管有没有空格都可以正确地返回结果了。
      

  10.   

    zcs_1(生生不息) ,你好。
    context
    "if a>b and a=100 then a=100;"
    "if a>b and a>1 then a=1;"
    "if a>b or a<1 then a=33;"
    "if a>b then a=133;"
    "if a>b then a=130;"
    这样sql的结果应该是:
    100100
    11
    133
      

  11.   

    select replace(regexp_substr(replace(context,' ',''),'[[:digit:]]*;$'),';','') from test;
    这个语句能够满足下面的需求。实际情况我需要用sql把数据查询出来,表中有很多数据,而且查询也有其他字段,只是其中有个string字段时这样的:
    context
    "if a>b and a=100 then a=100;"
    "if a>b and a>1 then a=1;"
    "if a>b or a<1 then a=33;"
    "if a>b then a=133;"
    "if a>b then a=130;"最后我想得到的结果是:最后一个数字,即最后一个分号和等号间的数字
    context
    100
    1
    33
    133
    130
      

  12.   

    >>正则表达式只能返回一个字符,但是实际有时候不能确定是几位哈。
    >>好像还是不能解决这个问题哦。能解决阿,你用+它就连续匹配,
    然后REGEXP_INSTR的第四个参数是1就返回第一个不符合的位置阿,
    不过阿,我没用过10g,是按照说明上的逻辑写的。
      

  13.   

    zcs_1(生生不息),以上是用你的SQL运行预计的结果。我发现我的oracle是9,不支持正则表达式
      

  14.   

    100100
    11
    133
    这种结果如果你直接REGEXP_REPLACE(context,'[^[:digit:]]')
    不就可以了?
      

  15.   

    谢谢大家,我请教了下我的师父,下面是他给出的答案:
    select trim(substr(
    context, 
    instr(context, '=', -1,1)+1,
    instr(context, ';', -1,1) - (instr(context, '=', -1,1)+1)))  from test