解决比较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没有换行符使 ...
随机推荐
- cocos 的CCScheduler模块
scheduleSelector函数->查找m_pHashForTimers是否有存储回调的Obj类实例,否,创建新条目tHashTimerEntry,指向回调类实例,tHashTimerEnt ...
- jQuery 左侧滑动
$("#slideleft button").click(function(){ var $lefty = $(this).next(); $lefty.animate({ lef ...
- Collision使用 获取其组件执行变色操作
using UnityEngine; using System.Collections; public class CyCollision : MonoBehaviour { void OnColli ...
- 一步一步教你做ios推送
最近在研究ios的推送问题,遇到了一些问题,最终整理了一下.放在这里和大家分享 APNS的推送机制 首先我们看一下苹果官方给出的对ios推送机制的解释.如下图 Provider就是我们自己程序的后台服 ...
- Hadoop 开源调度系统zeus(二)
紧跟之前Hadoop 开源调度系统zeus(一) 本节主要介绍一下zeus的架构: 先给一个zeus的架构图 无论Master还是Worker都有一套WEB UI,无论从哪个上面去看,看到的结果都是一 ...
- html 文字溢出标签
overflow:visible;作用:能看到溢出部分. overflow: hidden;作用:溢出部分看不到 overflow:scroll; 作用:出现一个滚动条(不超过的文字也会在滚动条里) ...
- SqlServer跨域查询
SELECT * FROM OPENDATASOURCE('SQLOLEDB','Data Source=192.168.1.14;User ID=sa;Password=sql.com').eBui ...
- Android系统中长按事件的实现机制解析
在Android的触摸消息中,已经实现了三种监测,它们分别是 1)pre-pressed:对应的语义是用户轻触(tap)了屏幕 2)pressed:对应的语义是用户点击(press)了屏幕 3)lon ...
- 动态更新UI的方式
1. TimerTask 和 timer连用: 这里主要是实现倒计时, TimerTask 里面有方法runOnUiThread,在这个方法里面调用timer cancel()停止倒计时,同样更新UI ...
- Spring MVC 之 Hello World
1.新建一个动态web项目 2.web.xml编写 <?xml version="1.0" encoding="UTF-8"?> <web-a ...