在数据库中表test,其中有一个string类型字段,如:context
"if a>b then a=100;"需要用SQL把string类型中得100这个数字取出来。例如:
select substr(context,-2)from test最后结果要为:
substr(context,-1)
100
"if a>b then a=100;"需要用SQL把string类型中得100这个数字取出来。例如:
select substr(context,-2)from test最后结果要为:
substr(context,-1)
100
解决方案 »
- oracle创建表时候指定表空间?
- Oracle多张表整成一张表
- ora-01008 并非所有变量都已关联的错误,是什么原因呀?
- ORACLE 时间字段排序问题,急需论坛高手解决!!!
- 如何打开oracle dbf文件 在线等。。。。(100分)
- 请大家看一下怎样写这个查询语句
- oracle 服务器版可以打包到installsheld里面吗?
- 急:学习Orace的Portal功能,哪里可以下载到相关资料?
- like %%在PL/SQL怎么用?特别是like一个变量的时候
- 请问“SELECT /*+ norewrite */ ...”中“/* ... */”中的语句是什么意思?是注释吗?
- 请问怎么定时地从一个数据库往另外一个数据库导数据?急急急。。。在线等
- 再问oracle强制指定索引时,oracle并不理会我的命令,请问高手如何解决?
不知道ORACLE自己有没有这个函数。
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.
实际情况我需要用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
然后用substr就可以直接出来了。select substr(context,
REGEXP_INSTR(context,'[[:digit:]]'),
REGEXP_INSTR(context,'[[:digit:]]+',1,1) - REGEXP_INSTR(context,'[[:digit:]]')
)from test
否则这sql一点可读性都没..
参考:
http://www.oracle.com/technology/global/cn/oramag/webcolumns/2003/techarticles/rischert_regexp_pt1.html
==>
for i in 255 .. 1
再加上一些控制代码
如果10g,最好象楼上所说的用正则表达式
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
这个语句能够满足下面的需求。实际情况我需要用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
>>好像还是不能解决这个问题哦。能解决阿,你用+它就连续匹配,
然后REGEXP_INSTR的第四个参数是1就返回第一个不符合的位置阿,
不过阿,我没用过10g,是按照说明上的逻辑写的。
11
133
这种结果如果你直接REGEXP_REPLACE(context,'[^[:digit:]]')
不就可以了?
select trim(substr(
context,
instr(context, '=', -1,1)+1,
instr(context, ';', -1,1) - (instr(context, '=', -1,1)+1))) from test