Home > Database > Mysql Tutorial > body text

Oracle提取字符串中“汉字”串的解决办法

WBOY
Release: 2016-06-07 16:42:43
Original
1677 people have browsed it

我们在实际工作中常常要使用去特殊字符的功能,特别是插入到数据库中的数据,我们为了保证数据的质量,往往需要对数据进行清洗,

我们在实际工作中常常要使用去特殊字符的功能,特别是插入到数据库中的数据,我们为了保证数据的质量,往往需要对数据进行清洗,就是去掉特殊字符,我们处理问题的思路是:判断字符串中的每个字符是不是中文,如果是中文,将他们按照顺序连接起来,如果不是,不要。

根据实际业务需求,共有2中解决方案:

方案一:

使用数据库自带函数length,lengthb。

中文下length返回的是字符个数,中文占1字符,lengthb返回的是字节个数,中文占2字节,根据中文的特性即可解决,但是实际情况往往不是特别理想,往往还存在着一些特殊字符,这些特殊字符和中文的字符数和字节数一致,这时我们就不能准确判断数据库中的中文字符了,为了解决这个问题,建议方案二。

方案二:

使用ASCII码来区分中文和其它字符,中文的ASCII码值的范围是45217~63486,根据这个来实现,,我们就需要使用Oracle中的函数ASCII,用它来返回ASCII值。

这种方式可以完美区分中文字符和其它字符。

create or replace function getCustText(custName varchar2) return varchar2 is
  Result varchar2(100);
  tmp_custName varchar2(100);
  count_str number;
  i number:=1;
  str_ascii number;
  current_char varchar2(10);
begin
    select length(custName) into count_str from dual;
    while i        current_char:=substr(custName,i,1);
        select ASCII(current_char) into str_ascii from dual;
        if str_ascii>45216 then
            tmp_custName:=tmp_custName||current_char;
        end if;
        i:=i+1;
    end loop;
    Result:=tmp_custName;
  return(Result);
end getCustText;

以上是Oracle中的实现方式,通过该方式可以快速,准确的识别中文字符。

在CentOS 6.4下安装Oracle 11gR2(x64)

Oracle 11gR2 在VMWare虚拟机中安装步骤

Debian 下 安装 Oracle 11g XE R2

Oracle Linux 6.5安装Oracle 11.2.0.4 x64

本文永久更新链接地址:

linux

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!