CREATE OR REPLACE Function IDCARD15TO18(CARD Varchar2) Return Varchar2 Is Type TIARRAY Is Table Of Integer; Type TCARRAY Is Table Of Char(1); RESULTS Varchar2(18); W TIARRAY; --数字数组 A TCARRAY; --字符数组 S Integer; Begin If CARD Is Null Then Return ''; End If; If LENGTH(CARD) <> 15 Then Return CARD; End If; W := TIARRAY(7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2, 1); A := TCARRAY('1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2'); RESULTS := SUBSTR(CARD, 1, 6) || '19' || SUBSTR(CARD, 7, 9); S := 0; Begin For I In 1 .. 17 Loop S := S + To_Number(SUBSTR(RESULTS, I, 1)) * W(I); End Loop; Exception When Others Then Return ''; End; S := S Mod 11; RESULTS := RESULTS || A(S + 1); Return(RESULTS); End IDCARD15TO18;
CREATE OR REPLACE Function IDCARD18TO15(CARD Varchar2) Return Varchar2 Is RESULTS Varchar2(18); Begin If CARD Is Null Then Return ''; End If; If LENGTH(CARD) <> 18 Then Return CARD; End If; RESULTS := substr(CARD,1,6)||substr(CARD,9,9); Return(RESULTS); End IDCARD18TO15;