oracle 对比 clob,解决比较Oracle中CLOB字段问题

Oracle中CLOB和BLOB字段虽说在开发中满足了存放超大内容的要求,但是在一些简单使用中确频频带来麻烦。CLOB中存放的是指针,并不能直接取到实际值。而SQLServer中的text字段就很方便,可以直接拿来与需要的字符串比对,象什么等于呀小于呀Like呀不在话下。可是换成Oracle就麻烦死了,要开辟一个缓存,把内容一段段读取出来后转换,难道写个where条件都这么复杂?经过多方寻求资料,终于发现一个方便简单的方法:利用dbms_lob 包中的方法(放心,内置的)instr和substr 。具体的介绍如下:

instr函数与substr函数

instr函数用于从指定的位置开始,从大型对象中查找第N个与模式匹配的字符串。

用于查找内部大对象中的字符串的instr函数语法如下:

1: dbms_lob.instr(

2: lob_loc in clob character set any_cs,

3: pattern in varchar2 character set lob_loc%charset,

4: offset in integer:=1,

5: nth in integer := 1)

6: return integer;

7:

lob_loc为内部大对象的定位器

pattern是要匹配的模式

offset是要搜索匹配文件的开始位置

nth是要进行的第N次匹配

substr函数

substr函数用于从大对象中抽取指定数码的字节。当我们只需要大对象的一部分时,通常使用这个函数。

操作内部大对象的substr函数语法如下:

其中各个参数的含义如下:

lob_loc是substr函数要操作的大型对象定位器

amount是要从大型对象中抽取的字节数

offset是指从大型对象的什么位置开始抽取数据。

如果从大型对象中抽取数据成功,则这个函数返回一个 raw 值。如果有一下情况,则返回null:

1 任何输入参数尾null

2 amount < 1

3 amount > 32767

4 offset < 1

5 offset > LOBMAXSIZE

1: dbms_lob.instr(

2: lob_loc in blob,

3: pattern in raw,

4: offset in integer := 1;

5: nth in integer := 1)

6: return integer;

7: dbms_lob.substr(

8: lob_loc in blob,

9: amount in integer := 32767,

10: offset in integer := 1)

11: return raw;

12: dbms_lob.substr(

13: lob_loc in clob character set any_cs,

14: amount in integer := 32767,

15: offset in integer := 1)

16: return varchar2 character set lob_loc%charset;

照下面示例,很容易看懂:

1: declare

2: source_lob clob;

3: pattern varchar2(6) := ‘Oracle‘;

4: start_location integer := 1;

5: nth_occurrence integer := 1;

6: position integer;

7: buffer varchar2(100);

8: begin

9: select clob_locator into source_lob from mylobs where lob_index = 4;

10: position := dbms_lob.instr(source_lob, pattern, start_location, nth_occurrence);

11: dbms_output.put_line(‘The first occurrence starts at position:‘ || position);

12:

13: nth_occurrence := 2;

14: select clob_locator into source_lob from mylobs where lob_index = 4;

15: position := dbms_lob.instr(source_lob, pattern, start_location, nth_occurrence);

16: dbms_output.put_line(‘The first occurrence starts at position:‘ || position);

17:

18: select clob_locator into source_lob from mylobs where lob_index = 5;

19: buffer := dbms_lob.substr(source_lob, 9, start_location);

20: dbms_output.put_line(‘The substring extracted is: ‘ || buffer);

21: end;

输出结果为:

The first occurrence starts at position:8

The first occurrence starts at position:24

The substring extracted is: Oracle 9i

哈哈,轻松搞定CLOB字段的比对匹配筛选,不用一大堆的存储过程和函数,祝大家好运!

原文:http://www.cnblogs.com/skyay/p/4249431.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值