解决比较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字段的比对匹配筛选,不用一大堆的存储过程和函数,祝大家好运!
解决比较Oracle中CLOB字段问题的更多相关文章
- oracle中clob字段的使用
oracle中定义了一个字段是clob的,由于用的是ssh的框架,结果在面向对象存取的时候出现clob类型字段和String类型字段的转换问题.开始查阅了clob字段和String字段的相互转换的方法 ...
- oracle中clob字段怎么查询非空列_20180517
select * from uap_groupsynlogvo a where a.log_msg is not null ; 附加demo的建表脚本跟业务数据. 链接:https://pan.bai ...
- Oracle中Clob类型处理解析:ORA-01461:仅可以插入LONG列的LONG值赋值
感谢原作者:破剑冰-Oracle中Clob类型处理解析 上一篇分析:ORA-01461: 仅能绑定要插入 LONG 列的 LONG 值 最近为Clob字段在插入数据时发现当字符的字节数(一个半角字符一 ...
- java 存储oracle的clob字段
项目中有很长的字符创需要存储,用到了oracle的clob字段,直接很长的字符串插入到clob字段中会报字符过长的异常,于是便寻求解决方案.看到这个博客写的还不错 首先,创建一个含CLOB字段的表: ...
- .Net处理Oracle中Clob类型字段总结
最近在做项目中用到Clob这个字段,Clob是存储无限长字符的Oracle字段,用的时候网上找资料找了好久,内容不是很多,大部分都不能用,当然也有可以用的,测试了不同版本,整理了一下,给大家在做项目的 ...
- Oracle中Clob类型处理解析
最近利用NHibernate映射类型为Clob字段在插入数据时发现当字符的字节数(一个半角字符一个字节,一个全角字符两个字节)在2000-4000之间时报错(ORA-01461:仅可以插入LONG列的 ...
- Oracle中Clob类型处理解析 (转)
转:原文:http://blog.csdn.net/pojianbing/article/details/2789426 最近利用NHibernate映射类型为Clob字段在插入数据时发现当 ...
- java 将长度很长的字符串(巨大字符串超过4000字节)插入oracle的clob字段时会报错的解决方案
直接很长的字符串插入到clob字段中会报字符过长的异常,相信大家都会碰到这种情况 String sql = "insert into table(request_id,table_name, ...
- Oracle数据库clob字段导出为sql insert插入语句
oracle数据库的clob字段导出为sql insert插入语句可以分三种情况:1,clob没有换行符:2,clob有换行符但不以分号结尾:3,clob有换行符并且以分号结尾. clob没有换行符使 ...
随机推荐
- 金错刀对话口袋购物王珂:找到痛点,确认卖点,制造爆点! - 资讯 - i黑马网
金错刀对话口袋购物王珂:找到痛点,确认卖点,制造爆点! - 资讯 - i黑马网 金错刀对话口袋购物王珂:找到痛点,确认卖点,制造爆点!
- 我的eclipse插件推荐
1. ER图工具 ERMaster - http://ermaster.sourceforge.net/update-site/ 优点:可根据数据库生成ER图.支持生成转换成PNG,JavaDOC ...
- 关于Python中的for循环控制语句
#第一个:求 50 - 100 之间的质数 import mathfor i in range(50, 100 + 1): for j in range(2, int(math.sqrt(i)) ...
- java类的加载顺序
related URL: http://www.cnblogs.com/guoyuqiangf8/archive/2012/10/31/2748909.html Parent Class: packa ...
- hand第四次考核
使用Spring与Mybatis技术实现下要求: (2分)1,Spring的配置文件名称为ApplicationContext.xml (2分)2,在ApplicationContext.xml中配置 ...
- css样式-表格优化
1.表格的初步优化 index.html <!DOCTYPE html> <html> <head> <meta charset="UTF-8&qu ...
- jTemplates——学习(1)
这里介绍一个基于jQuery开发的模板引擎. jTemplates目前最新的版本是0.7.8,由tPython开发.官方网站:http://jtemplates.tpython.com 两个附件, 一 ...
- office2007序列号/密钥
绝对可以用的许可证:V9MTG-3GX8P-D3Y4R-68BQ8-4Q8VD
- ajax弹出窗口
提取自ZCMS的弹出框: 代替window.open.window.alert.window.confirm:提供良好的用户体验: 水晶质感,设计细腻,外观漂亮: 兼容ie6/7/8.firefox2 ...
- LeetCode::Remove Duplicates from Sorted List II [具体分析]
Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numb ...