oracle中函数的使用

函数:
    作为表达式的一部分调用;
    必须在规范中包含 RETURN 子句;
    必须返回单个值;
    必须包含至少一条 RETURN 语句。

 

 

有这样的需求,根据身份证号,获得年龄

create or replace function getAge(p_IDcard varchar2) return integer is

IDcardlen integer;
IDcardyear integer;

begin

IDcardlen :=Length(p_IDcard);

if IDcardlen = 18 then
IDcardyear := to_number(substr(p_IDcard,7,4));
end if;
if IDcardlen = 15 then
IDcardyear := to_number('19'||substr(p_IDcard,7,2));
end if;

return to_number(to_char(sysdate,'yyyy'))-IDcardyear;


end getAge;


使用
select getAge(t.cardid) from tbflowpeople t where getAge(t.cardid)>=18 and getAge(t.cardid) <50;

posted on 2014-03-20 17:05  谭一丹  阅读(262)  评论(0编辑  收藏  举报

导航