一、問題說明
專注于為中小企業(yè)提供成都網(wǎng)站制作、成都網(wǎng)站設(shè)計(jì)、外貿(mào)營銷網(wǎng)站建設(shè)服務(wù),電腦端+手機(jī)端+微信端的三站合一,更高效的管理,為中小企業(yè)楊浦免費(fèi)做網(wǎng)站提供優(yōu)質(zhì)的服務(wù)。我們立足成都,凝聚了一批互聯(lián)網(wǎng)行業(yè)人才,有力地推動(dòng)了近1000家企業(yè)的穩(wěn)健成長,幫助中小企業(yè)通過網(wǎng)站建設(shè)實(shí)現(xiàn)規(guī)模擴(kuò)充和轉(zhuǎn)變。
在處理數(shù)據(jù)的時(shí)候,需要判斷某個(gè)字段字符串中是否有中文,若有則取出中文。
二、解決辦法
首先如何判斷某個(gè)字段字符串中是否有中文。這里介紹三種方法:
1、采用ASCIISTR函數(shù)
說明:ASCIISTR函數(shù)用于返回字符的ASCII形式的字符串;非ASCII的字符被轉(zhuǎn)化為\xxxx的形式。換句話說:如果字符中包含中文,則必定會有\(zhòng)xxxx的字符。
所以,我們直接利用ASCIISTR函數(shù)匹配'\'即可判斷。
2、采用length和lengthb的原理
說明:中文下length返回的是字符個(gè)數(shù),中文占1字符,lengthb返回的是字節(jié)個(gè)數(shù),中文占2字節(jié),根據(jù)中文的特性即可解決。
3、使用CONVERT函數(shù)判別
說明:CONVERT(要轉(zhuǎn)換的字符串,目標(biāo)字符集,原字符集),CONVERT函數(shù)用于轉(zhuǎn)換字符串的字符集;所以我們可以利用中文的字符集是utf-8來判斷。
上面介紹了如何判斷某個(gè)字段字符串中是否有中文,在成功判斷字段中是否有中文之后;那么該如何取出字段中的中文呢?
這里介紹一種快速準(zhǔn)確的辦法:
函數(shù)創(chuàng)建成功后,直接傳jldw調(diào)用就可獲取該字符的中文了。
源碼如下:
create or replace function getCustText(custName varchar2) return varchar2 is
Result varchar2(100); ? ?--返回的結(jié)果字符串
tmp_custName varchar2(100); ? --臨時(shí)變量
count_str number; ? ? --字符串中字符的個(gè)數(shù)
i number:=1; ? ? ? ? ?--循環(huán)變量
str_ascii number; ? ?--當(dāng)前等待判斷字符的ascii碼
current_char varchar2(10); ?--當(dāng)前等待判斷的字符
begin
select length(custName) into count_str from dual; ? ?--取出待處理字符串的長度
while icount_str loop ? ? ? ? ? ? ? ? ? ? ? ?---根據(jù)待處理字符串長度(counts)?,逐個(gè)字符判斷處理
current_char:=substr(custName,i,1);
select ASCII(current_char) into str_ascii from dual;
if str_ascii45216 then
tmp_custName:=tmp_custName||current_char;
end if;
i:=i+1;
end loop;
Result:=tmp_custName;
return(Result);
end getCustText;
select?to_char(to_date('2013年04月04日','yyyy年mm月dd日'),'yyyymmdd')
from?dual;
我嘗試著寫了一個(gè),如果都是只在前后有中文,那么這個(gè)應(yīng)該可以。你試試,我這里可沒有環(huán)境去測試。
SELECT regexp_substr(changpai, '[[:alnum:]]+') FROM huishou.dx_new_car;
從表里提取漢字, 需要考慮字符集, 不同的字符集漢字的編碼有所不同
這里以GB2312為例, 寫一函數(shù)準(zhǔn)確地從表里提取簡體漢字.
假設(shè)數(shù)據(jù)庫字符集編碼是GB2312, 環(huán)境變量(注冊表或其它)的字符集也是GB2312編碼
并且保存到表里的漢字也都是GB2312編碼的
那么也就是漢字是雙字節(jié)的,且簡體漢字的編碼范圍是
B0A1 - F7FE
換算成10進(jìn)制就是
B0 A1 F7 FE
176,161 - 247,254
我們先看一下asciistr函數(shù)的定義
Non-ASCII characters are converted to the form \xxxx, where xxxx represents a UTF-16 code unit.
但是這并不表示以 "\" 開始的字符就是漢字了
舉例如下
SQL select * from test;
NAME
--------------------
,啊OO10哈
你好aa
大家好aa/
☆大海123
★ABC
這里第5條記錄有一個(gè)實(shí)心的五角星
然后用asciistr函數(shù)轉(zhuǎn)換一下試試
SQL select name,asciistr(name) from test;
NAME ASCIISTR(NAME)
-------------------- ----------------------
,啊OO10哈 ,\554AOO10\54C8
你好aa \4F60\597Daa
大家好aa/ \5927\5BB6\597Daa/
☆大海123 \2606\5927\6D77123
★ABC \2605ABC
我們看到最后一條記錄的實(shí)心五角星也是 "\"開頭的
此時(shí)我們就不能用asciistr(字段)是否存在 "\" 來判斷是否含有漢字了.
我的函數(shù)如下,基本思路是判斷字符的編碼是否在GB2312規(guī)定的漢字編碼范圍之內(nèi)
[PHP]
create or replace function get_chinese(p_name in varchar2) return varchar2
as
v_code varchar2(30000) := '';
v_chinese varchar2(4000) := '';
v_comma pls_integer;
v_code_q pls_integer;
v_code_w pls_integer;
begin
if p_name is not null then
select replace(substrb(dump(p_name,1010),instrb(dump(p_name,1010),'ZHS16GBK:')),'ZHS16GBK: ','') into v_code from dual where rownum=1;
for i in 1..length(p_name) loop
if lengthb(substr(p_name,i,1))=2 then
v_comma := instrb(v_code,',');
v_code_q := to_number(substrb(v_code,1,v_comma-1));
v_code_w := to_number(substrb(v_code,v_comma+1,abs(instrb(v_code,',',1,2)-v_comma-1)));
if v_code_q=176 and v_code_q=247 and v_code_w=161 and v_code_w=254 then
v_chinese := v_chinese||substr(p_name,i,1);
end if;
v_code := ltrim(v_code,'1234567890');
v_code := ltrim(v_code,',');
end if;
v_code := ltrim(v_code,'1234567890');
v_code := ltrim(v_code,',');
end loop;
return v_chinese;
else
return '';
end if;
end;
/
.
[/PHP]
好,現(xiàn)在來執(zhí)行一些語句
SQL select * from test;
NAME
--------------------
,啊OO10哈
你好aa
大家好aa/
☆大海123
★ABC
5 rows selected.
1. 列出有漢字的記錄
SQL select name from test where length(get_chinese(name))0;
NAME
--------------------
,啊OO10哈
你好aa
大家好aa/
☆大海123
4 rows selected.
2. 列出有漢字的記錄,并且只列出漢字
SQL select get_chinese(name) from test where length(get_chinese(name))0;
GET_CHINESE(NAME)
---------------------------------------------------------------------------
啊哈
你好
大家好
大海
4 rows selected.
需要說明的是GB2312共有6763個(gè)漢字,即72*94-5=6763
我這里是計(jì)算72*94,沒有減去那5個(gè),那五個(gè)是空的。等查到了再減去
============
改寫這個(gè)函數(shù),可以提取非漢字或者漢字
該函數(shù)有兩個(gè)參數(shù),第一個(gè)表示要提取的字符串,第二個(gè)是1,表示提取漢字,是非1,表示提取非漢字
[PHP]
create or replace function get_chinese
(
p_name in varchar2,
p_chinese in varchar2
) return varchar2
as
v_code varchar2(30000) := '';
v_chinese varchar2(4000) := '';
v_non_chinese varchar2(4000) := '';
v_comma pls_integer;
v_code_q pls_integer;
v_code_w pls_integer;
begin
if p_name is not null then
select replace(substrb(dump(p_name,1010),instrb(dump(p_name,1010),'ZHS16GBK:')),'ZHS16GBK: ','') into v_code from dual where rownum=1;
for i in 1..length(p_name) loop
if lengthb(substr(p_name,i,1))=2 then
v_comma := instrb(v_code,',');
v_code_q := to_number(substrb(v_code,1,v_comma-1));
v_code_w := to_number(substrb(v_code,v_comma+1,abs(instrb(v_code,',',1,2)-v_comma-1)));
if v_code_q=176 and v_code_q=247 and v_code_w=161 and v_code_w=254 then
v_chinese := v_chinese||substr(p_name,i,1);
else
v_non_chinese := v_non_chinese||substr(p_name,i,1);
end if;
v_code := ltrim(v_code,'1234567890');
v_code := ltrim(v_code,',');
else
v_non_chinese := v_non_chinese||substr(p_name,i,1);
end if;
v_code := ltrim(v_code,'1234567890');
v_code := ltrim(v_code,',');
end loop;
if p_chinese = '1' then
return v_chinese;
else
return v_non_chinese;
end if;
else
return '';
end if;
end;
/
.
[/PHP]
SQL select * from a;
NAME
--------------------
我們啊、
他(艾呀)是★們
他的\啊@
SQL select get_chinese(name,1) from a;
GET_CHINESE(NAME,1)
-----------------------------------------
我們啊
他艾呀是們
他的啊
SQL select get_chinese(name,0) from a;
GET_CHINESE(NAME,0)
-----------------------------------------
、
()★
\@
SQL
網(wǎng)頁題目:oracle如何剔除中文,oracle正則表達(dá)式去除中文
網(wǎng)頁URL:http://www.muchs.cn/article32/hcpgsc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供定制開發(fā)、手機(jī)網(wǎng)站建設(shè)、域名注冊、網(wǎng)站制作、網(wǎng)站設(shè)計(jì)公司、網(wǎng)站建設(shè)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)