pl/sql版本的18位身份证号码生成算法

CREATE OR REPLACE FUNCTION FUN_GET_18CODE(p_15code IN VARCHAR2)
  RETURN VARCHAR2 IS
  p_18code VARCHAR2(20);
  p_15temp VARCHAR2(20);
  p_18temp VARCHAR2(20);
BEGIN
  p_18code := p_15code;
  IF lengthb(p_15code) = 15 THEN
    SELECT substr(p_15code, 1, 6) || '19' || substr(p_15code, 7, 9) ||
           decode(MOD(substr(p_15code, 1, 1) * 7 +
                      substr(p_15code, 2, 1) * 9 +
                      substr(p_15code, 3, 1) * 10 +
                      substr(p_15code, 4, 1) * 5 +
                      substr(p_15code, 5, 1) * 8 +
                      substr(p_15code, 6, 1) * 4 + 1 * 2 + 9 * 1 +
                      substr(p_15code, 7, 1) * 6 +
                      substr(p_15code, 8, 1) * 3 +
                      substr(p_15code, 9, 1) * 7 +
                      substr(p_15code, 10, 1) * 9 +
                      substr(p_15code, 11, 1) * 10 +
                      substr(p_15code, 12, 1) * 5 +
                      substr(p_15code, 13, 1) * 8 +
                      substr(p_15code, 14, 1) * 4 +
                      substr(p_15code, 15, 1) * 2,
                      11),
                  0,
                  '1',
                  1,
                  '0',
                  2,
                  'X',
                  3,
                  '9',
                  4,
                  '8',
                  5,
                  '7',
                  6,
                  '6',
                  7,
                  '5',
                  8,
                  '4',
                  9,
                  '3',
                  10,
                  '2')

      INTO p_18code
      FROM dual;

  ELSE
    p_18code := p_15code;
  END IF;
  RETURN p_18code;
EXCEPTION
  WHEN OTHERS THEN
    p_18code := p_15code;
    RETURN p_18code;
END fun_get_18code;

点赞