oracle中如何判断一个字符串是否含有汉字
一.1 BLOG文档结构图
650) this.width=650;" title="wps3D5D.tmp" border="0" alt="wps3D5D.tmp" src="http://s3.51cto.com/wyfs02/M02/6E/06/wKiom1VxP23zKq1CAAD_dmbe8j4132.jpg" "390" height="287" />' ref='nofollow'>
650) this.width=650;" title="wps3D5D.tmp" border="0" alt="wps3D5D.tmp" src="http://s3.51cto.com/wyfs02/M02/6E/06/wKiom1VxP23zKq1CAAD_dmbe8j4132.jpg" "390" height="287" />
一.2 前言部分
一.2.1 导读
各位技术爱好者,看完本文后,你可以掌握如下的技能,也可以学到一些其它你所不知道的知识,~O(∩_∩)O~:
①全角字符的判断,或者是含有汉字的字符串的判断
本文如有错误或不完善的地方请大家多多指正,ITPUB留言或QQ皆可,您的批评指正是我写作的最大动力。
一.2.2 实验环境介绍
11.2.0.3 RHEL6.5
一.2.3 本文简介
看到网友问,怎么查询表中某个字段数据是不是包含了全角字符啊? 这个问题涉及到几个个函数:to_single_byte、length和lengthb,我之前做开发的时候研究的是如何判断一个字符串中是否包含中文,其实和这个本质是一样的,且看实验部分。
650) this.width=650;" title="wps3D5E.tmp" border="0" alt="wps3D5E.tmp" src="http://s3.51cto.com/wyfs02/M00/6E/06/wKiom1VxP26Sd3NXAAB4lz_bWuU051.jpg" "419" height="133" />' ref='nofollow'>
650) this.width=650;" title="wps3D5E.tmp" border="0" alt="wps3D5E.tmp" src="http://s3.51cto.com/wyfs02/M00/6E/06/wKiom1VxP26Sd3NXAAB4lz_bWuU051.jpg" "419" height="133" />
一.3 实验部分
一.3.1 lengthb和length函数结合to_single_byte函数
---含有汉字,严格的说是含有全角字符
SELECT l.name,
length(l.name),
lengthb(l.name)
FROM xb_link l
WHERE length(l.name) != lengthb(l.name)
AND length(l.name) < 20;
650) this.width=650;" title="wps3D6E.tmp" border="0" alt="wps3D6E.tmp" src="http://s3.51cto.com/wyfs02/M01/6E/06/wKiom1VxP26gr9_kAAEb6NxNpPk658.jpg" "503" height="190" />' ref='nofollow'>
650) this.width=650;" title="wps3D6E.tmp" border="0" alt="wps3D6E.tmp" src="http://s3.51cto.com/wyfs02/M01/6E/06/wKiom1VxP26gr9_kAAEb6NxNpPk658.jpg" "503" height="190" />
以下数据也满足条件:
SELECT l.id,
l.name
FROM xb_link l
WHERE length(l.name) != lengthb(l.name)
AND l.metacategory IN
(‘com.gxlu.ngrm.network.DDNCircuit‘,
‘com.gxlu.ngrm.network.FRCircuit‘,
‘com.gxlu.ngrm.network.ATMCircuit‘,
‘com.gxlu.ngrm.network.DDNOCircuit‘,
‘com.gxlu.ngrm.network.FROCircuit‘)
AND l.id IN (‘301898331‘, ‘301898335‘, ‘301908187‘, ‘301929403‘);
650) this.width=650;" title="wps3D6F.tmp" border="0" alt="wps3D6F.tmp" src="http://s3.51cto.com/wyfs02/M02/6E/02/wKioL1VxQQzyTnWGAABsxx-UWmQ425.jpg" "245" height="89" />' ref='nofollow'>
650) this.width=650;" title="wps3D6F.tmp" border="0" alt="wps3D6F.tmp" src="http://s3.51cto.com/wyfs02/M02/6E/02/wKioL1VxQQzyTnWGAABsxx-UWmQ425.jpg" "245" height="89" />
所以可以借助to_single_byte函数来解决。
SELECT l.id,
l.name,
to_single_byte(l.name),
length(l.name) l1,
lengthb(l.name) l2,
length(to_single_byte(l.name)) l
FROM xb_link l
WHERE length(l.name) != lengthb(l.name)
AND l.metacategory IN
(‘com.gxlu.ngrm.network.DDNCircuit‘,
‘com.gxlu.ngrm.network.FRCircuit‘,
‘com.gxlu.ngrm.network.ATMCircuit‘,
‘com.gxlu.ngrm.network.DDNOCircuit‘,
‘com.gxlu.ngrm.network.FROCircuit‘)
AND l.id IN (‘301898331‘, ‘301898335‘, ‘301908187‘, ‘301929403‘);
650) this.width=650;" title="wps3D70.tmp" border="0" alt="wps3D70.tmp" src="http://s3.51cto.com/wyfs02/M00/6E/02/wKioL1VxQQ3zEdiQAADitIF8VqE682.jpg" "530" height="90" />' ref='nofollow'>
650) this.width=650;" title="wps3D70.tmp" border="0" alt="wps3D70.tmp" src="http://s3.51cto.com/wyfs02/M00/6E/02/wKioL1VxQQ3zEdiQAADitIF8VqE682.jpg" "530" height="90" />
一.3.2 regexp_replace --替换其它字符为’’
SELECT
l.id,
l.name
FROM xb_link l
WHERE regexp_replace(TRIM(l.name),
‘([A-Za-z0-9]|[[:punct:]]|[[:space:]])‘,
‘‘) IS NOT NULL;
650) this.width=650;" title="wps3D71.tmp" border="0" alt="wps3D71.tmp" src="http://s3.51cto.com/wyfs02/M01/6E/02/wKioL1VxQQ2AZ3TvAAEiDFLxTfg717.jpg" "472" height="106" />' ref='nofollow'>
650) this.width=650;" title="wps3D71.tmp" border="0" alt="wps3D71.tmp" src="http://s3.51cto.com/wyfs02/M01/6E/02/wKioL1VxQQ2AZ3TvAAEiDFLxTfg717.jpg" "472" height="106" />
一.3.3 to_multi_byte 函数--全是全角字符
---全是汉字
SELECT l.name,
to_multi_byte(l.name)
FROM xb_link l
WHERE l.name = to_multi_byte(l.name);
650) this.width=650;" title="wps3D82.tmp" border="0" alt="wps3D82.tmp" src="http://s3.51cto.com/wyfs02/M02/6E/02/wKioL1VxQQ7xnUpoAABjT-424pw874.jpg" "256" height="85" />' ref='nofollow'>
650) this.width=650;" title="wps3D82.tmp" border="0" alt="wps3D82.tmp" src="http://s3.51cto.com/wyfs02/M02/6E/02/wKioL1VxQQ7xnUpoAABjT-424pw874.jpg" "256" height="85" />
SELECT l.name,
to_multi_byte(l.name)
FROM xb_link l;
650) this.width=650;" title="wps3D83.tmp" border="0" alt="wps3D83.tmp" src="http://s3.51cto.com/wyfs02/M00/6E/02/wKioL1VxQQ7wqI3GAABQED7v6cw554.jpg" "256" height="103" />' ref='nofollow'>
650) this.width=650;" title="wps3D83.tmp" border="0" alt="wps3D83.tmp" src="http://s3.51cto.com/wyfs02/M00/6E/02/wKioL1VxQQ7wqI3GAABQED7v6cw554.jpg" "256" height="103" />
一.3.4 网友问题解答:某个字段数据是不是包含了全角字符?
drop table aa;
create table aa (col varchar2(255));
SELECT * FROM aa for update;
650) this.width=650;" title="wps3D84.tmp" border="0" alt="wps3D84.tmp" src="http://s3.51cto.com/wyfs02/M01/6E/02/wKioL1VxQQ6ikGfPAABWmk5r3v0041.jpg" "221" height="103" />' ref='nofollow'>
650) this.width=650;" title="wps3D84.tmp" border="0" alt="wps3D84.tmp" src="http://s3.51cto.com/wyfs02/M01/6E/02/wKioL1VxQQ6ikGfPAABWmk5r3v0041.jpg" "221" height="103" />
SELECT a.col,
to_single_byte(a.col),
length(a.col),
lengthb(a.col),
length(to_single_byte(a.col)),
lengthb(to_single_byte(a.col))
FROM aa a
WHERE (lengthb(a.col) - length(a.col)) <>
(lengthb(to_single_byte(a.col)) - length(to_single_byte(a.col)));
650) this.width=650;" title="wps3D85.tmp" border="0" alt="wps3D85.tmp" src="http://s3.51cto.com/wyfs02/M02/6E/02/wKioL1VxQQ_Texf9AAEEGecJRMk511.jpg" "1148" height="69" />' ref='nofollow'>
650) this.width=650;" title="wps3D85.tmp" border="0" alt="wps3D85.tmp" src="http://s3.51cto.com/wyfs02/M02/6E/02/wKioL1VxQQ_Texf9AAEEGecJRMk511.jpg" "1148" height="69" />
一.4 总结
方法很简单,网友可能还有其他的办法,欢迎留言,对于不同的场景处理方式有很多种,我们应该学会灵活变通。
一.5 About Me
...........................................................................................................................................................................................
本文作者:小麦苗,只专注于数据库的技术,更注重技术的运用
ITPUB BLOG:http://blog.itpub.net/26736162
本文地址:http://blog.itpub.net/26736162/viewspace-1688209/
本文pdf版:http://yunpan.cn/QCwUAI9bn7g7w 提取码:af2d
QQ:642808185 若加QQ请注明你所正在读的文章标题
创作时间地点:2015-06-05 10:00~ 2015-06-05 13:00 于外汇交易中心
<版权所有,文章允许转载,但须以链接方式注明源地址,否则追究法律责任!>
...........................................................................................................................................................................................
原文:http://lhrbest.blog.51cto.com/2318295/1658869