看到网友问,怎么查询表中某个字段数据是不是包含了全角字符啊? 这个问题涉及到几个函数:to_single_byte、length和lengthb,我之前做开发的时候研究的是如何判断一个字符串中是否包含中文,其实和这个本质是一样的,且看实验部分。
1 实验部分
1.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;
以下数据也满足条件:
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');
所以可以借助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');
1.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;
1.3 to_multi_byte 函数--全是全角字符
---全是汉字
SELECT l.name,
to_multi_byte(l.name)
FROM xb_link l
WHERE l.name = to_multi_byte(l.name);
SELECT l.name,
to_multi_byte(l.name)
FROM xb_link l;
1.4 网友问题解答:某个字段数据是不是包含了全角字符?
我们做如下的实验:
drop table aa;
create table aa (col varchar2(255));
SELECT * FROM aa for update;
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)));