写一个sqlserver中的身份证15位转18位的函数怎么写?或者15位18位互换的函数。

解决方案 »

  1.   

    Function IDCode(sCode15 As String) As String 
      ' 功能:将15的身份证号升为18位(根据GB 11643-1999) 
      ' 参数:原来的号码 
      ' 返回:升位后的18位号码 
      Dim i,num As Integer 
      Dim code As String 
      num = 0 
      IDCode = Left(sCode15, 6) + "19" + Right(sCode15, 9) 
      ' 计算校验位 
      For i = 18 To 2 Step -1 
       num = num + (2 ^ (i - 1) Mod 11) * (Mid(IDCode, 19 - i, 1)) 
      Next i 
      num = num Mod 11 
      Select Case num 
      Case 0 
       code = "1" 
      Case 1 
       code = "0" 
      Case 2 
       code = "X" 
      Case Else 
       code = Trim(Str(12 - num)) 
      End Select 
      IDCode = IDCode + code 
    End Function
      

  2.   

    oracle的.--从原有的15位身份证号转换成新的18位(附PASCAL代码)
    function IDENTITYCODE15TO18(p_OldID varchar2) return varchar2 is 
    -- Author : XJG([email protected]
    -- Created : 2003-11-03 18:38:56 
    -- Purpose : 从原有的15位身份证号转换成新的18位
    type TIArray is table of integer; 
    type TCArray is table of char(1); 
    Result varchar2(18); 
    W TIArray; 
    A TCArray; 
    S integer; 
    begin 
    if Length(p_OldID) <> 15 then 
    raise_application_error(-20999, '不是旧15位身份证号'); 
    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'); 
    Result := SubStr(p_OldID, 1, 6) || '19' || SubStr(p_OldID, 7, 9); S := 0; 
    begin 
    for i in 1 .. 17 loop 
    S := S + to_number(SubStr(Result, i, 1)) * W(i); 
    end loop; 
    exception 
    when others then 
    return ''; 
    end; 
    S := S mod 11; 
    Result := Result || A(s + 1); return(Result); 
    end IDENTITYCODE15TO18;