日期:2014-05-16  浏览次数:20327 次

自定义oracle function 函数的应用.!!
最近在做一个东西,查询字母在某个位置的数据..以下是我的做法.和大家分享下.如果有更好的方法,希望大家也能分享下.多交流多学习嘛..


--函数1 
--作用  判断是否为数字
create or replace function isNumber(p_in varchar2) return varchar2 as
  i number;
begin
 --是否有E字母,因为程序会将字母 E 解析为数字 如果有,直接返回FALSE
  if(instr(p_in,'E') = 0) then
      i := to_number(p_in);
      return 'TRUE';
  else 
       return 'FALSE';
  end if;
exception
  when others then
    return 'FALSE';
end;


--函数2
--作用 判断第几位为字符
create or replace function findCharToPostion(p_in varchar2, p_positon number)
 return  varchar2 as
  c char(1);
  i number;
begin
  select substr(p_in,p_positon,1) into c from dual;
  i := to_number(c);
  return 'FALSE';
  exception 
     when others then
      return 'TRUE';
end findCharToPostion;



使用方法:

--找出[Column]第三位为字符的所有数据
select * from TABLE where findCharToPostion(Column,3) = 'TRUE';
--找出[Column]第四位为字符的所有数据
select * from TABLE where findCharToPostion(Column,4) = 'TRUE';



只是一个简单的例子,我想说的是,有时候函数也能帮我们解决很多头疼的问题...!!

希望能对大家有所启发..!!!!