Home > Database > Mysql Tutorial > body text

Oracle自定义函数f_henry_GetStringLength

WBOY
Release: 2016-06-07 17:53:37
Original
1212 people have browsed it

Oracle下的Length()函数不能区分中英文,只能得到字符数而不能得到字节数,很多时候插入string到字段中时总要先进行一次检查,防止string长度超过了字段定义的长度,一般大家都是把这个检测放到应用程序中执行,用我写的这个小函数就可以实现在数据库服务器端

Oracle下的Length()函数不能区分中英文,只能得到字符数而不能得到字节数,很多时候插入string到字段中时总要先进行一次检查,防止string长度超过了字段定义的长度,一般大家都是把这个检测放到应用程序中执行,用我写的这个小函数就可以实现在端对要插入的字段进行检测。

create or replace function f_henry_GetStringLength(pv_String in varchar2) return integer is
  Result integer;
  i number;
begin
  Result:=0;
  if length(pv_String)=0 then
    return(Result);
  end if;
  for i in 1 .. length(pv_String) loop
    if ascii(substr(pv_String,i,1))      Result:=Result+1;
    else
      Result:=Result+2;
    end if;
  end loop;
  return(Result);
end f_henry_GetStringLength;

/*************************以下是测试***************************/

SQL> select length('啊$@oii发大幅') from dual;

LENGTH('啊$@OII发大幅')
-----------------------
                      9

SQL> select f_henry_GetStringLength('啊$@oii发大幅') from dual;

F_HENRY_GETSTRINGLENGTH('啊$@O
------------------------------
                            13

Related labels:
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!