1、判斷是否為數字
create or replace function is_number(string varchar2) return integer is
begin
if(length(trim(translate(string, ' +-.0123456789', ' '))) is null ) then
return 1;
else
return 0;
end if;
end is_number;
2、判斷是否為日期
create or replace function is_date(parmin varchar2) return number is
val date;
begin
val := to_date(nvl(parmin, 'a'), 'yyyy-mm-dd hh24:mi:ss');
return 1;
exception
when others then
return 0;
end;
3、判斷是否為身份證號碼
create or replace function is_idcard
(p_idcard varchar2)
return boolean
isidcardlen integer default 0;
begin
idcardlen:=length(p_idcard);
if (idcardlen = 18 and is_number(substr(p_idcard, 1, idcardlen-1))=1
and is_date(substr(p_idcard,7,8))=1)
or(idcardlen = 15 and is_number(substr(p_idcard, 1, idcardlen))=1
and is_date ('19' || substr(p_idcard,7,6))=1)
then
return true;
else
return false;
end if;
end is_idcard;
4、從身份證號中獲取年齡
create or replace function get_age
(workerid in varchar2)
return varchar2
isagevalue varchar2(10);
begin
if(length(trim(workerid))=18) then
select (to_char(sysdate,'yyyy')-to_char(substr(workerid,7,4))) into agevalue from dual;
else if (length(trim(workerid))=15) then
select (to_char(sysdate,'yyyy')-to_char('19'||substr(workerid,7,2))) into agevalue from dual;
else
agevalue:='0';
end if;
end if;
return agevalue;
end;
5、從身份證號碼中獲取性別
create or replace function get_***(p_idcard varchar2)
return varchar2
isidcardlen integer;
begin
idcardlen:=length(p_idcard);
if is_idcard(p_idcard)=false then
return null;
end if;
if idcardlen=18 and substr(p_idcard,17,1) in (1,3,5,7,9) then
return ('男');
end if;
if idcardlen=18 and substr(p_idcard,17,1) in (2,4,6,8,0) then
return ('女');
end if;
if idcardlen=15 and substr(p_idcard,15,1) in (1,3,5,7,9) then
return ('男');
end if;
if idcardlen=15 and substr(p_idcard,15,1) in (2,4,6,8,0) then
return ('女');
end if;
end get_***;
不斷更新中……
Oracle自定義函式
語法如下 create or replace function function name argment type,argment type return return type 返回資料的型別 變數的申明,比如 stryuan varchar2 150 begin function body 函...
oracle 自定義函式
下面是乙個前輩寫的判斷是否是手機號的函式 create or replace function ismobile pmsg varchar2 return number isvprefix varchar2 20 vlen number begin vlen lengthb pmsg if vlen...
Oracle自定義函式
二 刪除自定義函式 三 應用經驗 在oracle資料庫中,為了實現特定的功能,可以自定義函式,就像c c 語言,除了系統的庫函式,程式設計師還會編寫很多自定義的函式。create or replace function 函式名 引數1 模式 資料型別,return 資料型別 as 定義區域性變數。變...