oracle自定义函数身份证15位和18位的转换

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;
 /

 

 

posted @ 2020-12-28 09:35  晨起的太阳  阅读(1219)  评论(0编辑  收藏  举报