[Switch] Oracle custom function syntax and examples

Original address: https://blog.csdn.net/libertine1993/article/details/47264211

The syntax of an Oracle custom function is as follows:

[sql] view plaincopy   
  1. create  or  replace  function function name (parameter 1 mode parameter type)  
  2. return return value type  
  3. as  
  4. variable1 variable type;  
  5. variable2 variable type;  
  6. begin  
  7.     function body;  
  8. end function name;  

 

There are 3 modes of parameters: (If not specified, the default type of the parameter is in.)

in : read-only mode, in the function, the value of the parameter can only be referenced and cannot be changed;

out : In write-only mode, it can only be assigned, not referenced;

in out : Readable and writable.

Reminder:
1. In Oracle custom functions, the correct way to write else if is elsif instead of else if
2. To use if, you need to add then "if condition then operation"

 

Simple example:  read in two values, return the larger value

[sql] view plaincopy   
  1. create or replace function function1(para1 in number, para2 in number)   
  2. return number   
  3. as   
  4. begin  
  5.   if para1 > para2 then  
  6.       return para1;  
  7.   else  
  8.       return para2;   
  9.   end if;  
  10. end function1;  

use:

[sql] view plaincopy   
  1. select function1(666, 333) from dual;  


result:

 

Another example may be used in practice. Sometimes we need to perform data statistics by non-natural months. In this case, we only need to write a custom function to process the date.

Function description: Read in date type date, those greater than the 15th are included in the next month, those less than or equal to the 15th are included in this month

[sql] view plaincopy   
  1. create or replace function fn_mymonth(oridate in date)  
  2. return varchar2  
  3. as  
  4.   oriday number;  
  5.   orimonth number;  
  6.   oriyear number;   
  7. begin  
  8.   oriday := to_number(to_char(oridate, 'dd'));  
  9.   orimonth := to_number(to_char(oridate, 'mm'));  
  10.   oriyear := to_number(to_char(oridate, 'yyyy'));  
  11.    
  12.   if oriday <= 15  then                               -- less than or equal to the 15th belongs to this month  
  13.     return to_char(oridate, 'yyyymm');  
  14.   else  
  15.     if orimonth <= 8  then                            -- day <= 8, then after +1, turn to char and make up 0, which is used separately as a case  
  16.       return to_char (oriyear) || '0' || to_char (orimonth + 1);  
  17.     elsif( orimonth <= 11 )  then                     -- day <= 11, then +1 will not cross the new year, and it is not necessary to fill in zeros when converting to char, and it is separated as a case  
  18.       return to_char (oriyear) || to_char (orimonth + 1);  
  19.     else                                             -- the last case is a new year, change the year, just fill the month with zeros  
  20.       return to_char (oriyear + 1) || '0' || to_char (orimonth - 11);  
  21.     end if;  
  22.   end if;  
  23. end fn_mymonth;  


Use and results:

[sql] view plaincopy   
  1. select fn_mymonth(to_date('2015-12-14', 'yyyy-mm-dd')) from dual;  

[sql] view plaincopy   
  1. select fn_mymonth(to_date('2015-12-15', 'yyyy-mm-dd')) from dual;  

 

[sql] view plaincopy   
  1. select fn_mymonth(to_date('2015-12-16', 'yyyy-mm-dd')) from dual;  

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325161397&siteId=291194637