SQL gets age based on ID number function

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.