在db2中,截取字段中的一些文字

博客介绍了在DB2中查询某个字段特定位置内容的方法,以班组中存储的XML文档为例,选用DB2的locate函数获取特定字符串的开始和结束位置,再用substr函数截取,还给出了两种具体实现方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

查某个字段中的特定位置中的内容,如班组中的存的一些xml文档。
我选用了db2的locate函数,该函数返回在给定字符串中 第一次出现特定子串的位置,通过locate函数我得到了特定字符串的开始和结束位置,在用substr截取,就ok了
下面是实现的两种方法,方法一和方法二没有质的区别
--方法一
select       substr(
substr(notecontent,200,100),
6+locate('<形式>',substr(notecontent,200,100)),
locate('</形式>',substr(notecontent,200,100))-6-locate('<形式>',substr(notecontent,200,100)))
 from USERID.T_NOTE_CULTURE

--方法二
with temp as(
select NOTEID,
locate('<形式>',substr(notecontent,200,100)) as begin,
locate('</形式>',substr(notecontent,200,100)) as lth
from
userid.T_note_culture

)
select temp.begin,temp.lth,substr(notecontent,205+temp.begin,temp.lth-temp.begin-6) 
from temp ,userid.T_note_culture a where a.noteid=temp.noteid

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值