oracle 函式的應用

2021-06-26 14:41:28 字數 3390 閱讀 3126

--返回日期型

create or replace function getplandate(v_date in date,v_cnt in number)--傳入當前時間,期數

return date

asy number;

m number;

d number;

last_cnt_date date;

last_cnt_end_date date;

last_cnt_end_day number;

begin

y := to_char(v_date,'yyyy');--獲取當前時間的年份

m := to_char(v_date,'mm');--獲取當前時間的月份

d := to_char(v_date,'dd');--獲取當前時間的日期

last_cnt_date := add_months(sysdate,v_cnt);--當前時間追加v_cnt個月的時間格式為 日-月-年

last_cnt_end_date := last_day(last_cnt_date);--last_cnt_date時間的最後一天,時間格式為 日-月-年

last_cnt_end_day := to_char(last_cnt_end_date,'dd');--獲取last_cnt_end_date 的日

if (to_char(last_day(v_date),'yyyy-mm-dd')=to_char(v_date,'yyyy-mm-dd')) then--當前時間是否為當前月份的最後一期

return last_cnt_end_date;

else

if (last_cnt_end_day <= d) then --判斷last_cnt_end_date 的日小於當前時間

return last_cnt_end_date;

else

return to_date(to_char(last_cnt_date,'yyyy-mm')||'-'||d,'yyyy-mm-dd');

end if;

end if;

end;

--返回字串型

p_endstatus                in varchar2,

p_endattachedstatus        in varchar2,

p_credit_endattachedstatus in varchar2,

p_nodecode                 in varchar2,

p_times in number)

return varchar2 --  表示函式的返回型別為varchar2型別

is -- 表示函式體部分

showresult varchar2(100);--  宣告零時變數,這是可有可無的,這裡宣告返回變數為varchar2型別的showresult變數。

begin

if p_endstatus is not null and p_endattachedstatus is null and p_nodecode is not null  then

select   id||','||to_char(time,'yyyy/mm/dd hh24:mi:ss') into showresult

from (

select rownum recno, n.* from

(select a.*

and endstatus = p_endstatus

elsif p_endstatus is null and p_endattachedstatus is not null and p_nodecode is not null   then

select id||','||to_char(time,'yyyy/mm/dd hh24:mi:ss') into showresult

from (

select rownum recno, n.* from

(select a.*

and endattachedstatus = p_endattachedstatus

elsif p_endstatus is null and p_endattachedstatus is null and

p_credit_endattachedstatus is not null and p_nodecode is not null then

select id||','||to_char(time,'yyyy/mm/dd hh24:mi:ss') into showresult

from (

select rownum recno, n.* from

(select a.*

and credit_endattachedstatus = p_credit_endattachedstatus

else

select id||','||to_char(time,'yyyy/mm/dd hh24:mi:ss') into showresult

from (

select rownum recno, n.* from

(select a.*

and endstatus = p_endstatus

end if;

return showresult;

end ntime;

--for迴圈更新sql,無返回值

create or replace function updatebank return varchar2 as

r re_contract_payinfo%rowtype;

begin

from re_contract_payinfo payinfo

if r.bank = '301' then--301

elsif r.bank = '305' then

elsif r.bank = '303' then

end if;

end loop;

return null;

end updatebank;

--for迴圈更新sql,有返回值

begin

if r.endstatus='010607' then

return '1';

elsif r.endstatus='010701' then

return '2';

elsif r.endstatus='010508' then

return '3';

elsif r.endstatus='100001' then

return '4';

end if;

end loop;

return '5';

end getclientstatus;

Oracle中DECODE函式的應用

decode函式相當於一條件語句 if 它將輸入數值與函式中的引數列表相比較,根據輸入值返回乙個對應值。函式的引數列表是由若干數值及其對應結果值組成的若干序偶形式。當然,如果未能與任何乙個實參序偶匹配成功,則函式也有預設的返回值。區別於sql的其它函式,decode函式還能識別和操作空值.其具體的語...

oracle 函式應用實踐一

create or replace function f get fjkywtce fcd in varchar,flag in number 非集客業務提成額 不參與考核分配 return number is prekpi number p1 number p2 number p3 number ...

Oracle 字元函式應用 檢測漢字

驗證是否包含漢字 create table test a varchar2 10 insert into test values 鳥 insert into test values 深刻 insert into test values aaa insert into test values bbb ...