In the application process of the database, we often encounter according to the ID number to get the current age, today I wrote a function here, is about getting the age of
Create or Replace functionFUNC_COMPARE_SFZHM (SFZHMinch varchar2)--Enter your ID number return Number /*function Description, the return value of 0 is less than 18 years of age, the value of 1 is greater than 75 years old, the other is the return value of 2 in this function used Trunc (Months_between ()) and then is the ID number of the month and day combined into a ' yyyy-mm-dd ' format, But splicing in SQL is a "| |"instead of "+", which is worth noting*/ isbegin ifTrunc (Months_between (To_date (To_char (Sysdate,'YYYY-MM-DD'),'YYYY-MM-DD'), To_date (substr (SFZHM,7,4)||'-'||SUBSTR (SFZHM, One,2)||'-'||SUBSTR (SFZHM, -,2),'YYYY-MM-DD'))/ A)< - Then return 0; Elsif Trunc (Months_between (to_date (To_char,'YYYY-MM-DD'),'YYYY-MM-DD'), To_date (substr (SFZHM,7,4)||'-'||SUBSTR (SFZHM, One,2)||'-'||SUBSTR (SFZHM, -,2),'YYYY-MM-DD'))/ A)> the Then return 1; Else return 2; End if;End;
The following is the SQL statement for the function call:
Select func_compare_sfzhm ('310103200003141234' from dual;
SQL gets age based on ID number function