日期:2014-05-18 浏览次数:20718 次
--将15位身份证升级成18位的用户定义函数 CREATE FUNCTION ID15TO18 (@id15 char(15)) RETURNS CHAR(18) AS BEGIN DECLARE @ID18 CHAR(18) DECLARE @S1 AS INTEGER DECLARE @S2 AS INTEGER DECLARE @S3 AS INTEGER DECLARE @S4 AS INTEGER DECLARE @S5 AS INTEGER DECLARE @S6 AS INTEGER DECLARE @S7 AS INTEGER DECLARE @S8 AS INTEGER DECLARE @S9 AS INTEGER DECLARE @S10 AS INTEGER DECLARE @S11 AS INTEGER DECLARE @S12 AS INTEGER DECLARE @S13 AS INTEGER DECLARE @S14 AS INTEGER DECLARE @S15 AS INTEGER DECLARE @S16 AS INTEGER DECLARE @S17 AS INTEGER DECLARE @S18 AS INTEGER SET @S1 = SUBSTRING(@ID15,1,1) SET @S2 = SUBSTRING(@ID15,2,1) SET @S3 = SUBSTRING(@ID15,3,1) SET @S4 = SUBSTRING(@ID15,4,1) SET @S5 = SUBSTRING(@ID15,5,1) SET @S6 = SUBSTRING(@ID15,6,1) SET @S7 = 1 SET @S8 = 9 SET @S9 = SUBSTRING(@ID15,7,1) SET @S10 = SUBSTRING(@ID15,8,1) SET @S11 = SUBSTRING(@ID15,9,1) SET @S12 = SUBSTRING(@ID15,10,1) SET @S13 = SUBSTRING(@ID15,11,1) SET @S14 = SUBSTRING(@ID15,12,1) SET @S15 = SUBSTRING(@ID15,13,1) SET @S16 = SUBSTRING(@ID15,14,1) SET @S17 = SUBSTRING(@ID15,15,1) SET @S18 = ( (@S1*7) + (@S2*9) + (@S3*10) + (@S4*5) + (@S5*8) + (@S6*4) + (@S7*2) + (@S8*1) + (@S9*6) + (@S10*3) + (@S11*7) + (@S12*9) + (@S13*10) + (@S14*5) + (@S15*8) + (@S16*4) + (@S17*2) ) % 11 SET @ID18 = SUBSTRING(@ID15,1,6) + '19' + SUBSTRING(@ID15,7,9) + CASE WHEN @S18 = 0 THEN '1' WHEN @S18 = 1 THEN '0' WHEN @S18 = 2 THEN 'X' WHEN @S18 = 3 THEN '9' WHEN @S18 = 4 THEN '8' WHEN @S18 = 5 THEN '7' WHEN @S18 = 6 THEN '6' WHEN @S18 = 7 THEN '5' WHEN @S18 = 8 THEN '4' WHEN @S18 = 9 THEN '3' WHEN @S18 = 10 THEN '2' END RETURN @ID18 END GO --调用函数 update 表 set 身份证号 = dbo.ID15TO18(身份证号) where LEN(身份证号) = 15
------解决方案--------------------
-- 15位身份证号升级为18位,适用于18xx年出生的公民 UPDATE 员工表 SET 身份证号= SUBSTRING(身份证号,1,6)+'18'+SUBSTRING(身份证号,7,9)+ SUBSTRING('10X98765432', ( CAST(SUBSTRING(身份证号, 1,1) AS INT)*7 +CAST(SUBSTRING(身份证号, 2,1) AS INT)*9 +CAST(SUBSTRING(身份证号, 3,1) AS INT)*10 +CAST(SUBSTRING(身份证号, 4,1) AS INT)*5 +CAST(SUBSTRING(身份证号, 5,1) AS INT)*8 +CAST(SUBSTRING(身份证号, 6,1) AS INT)*4 +1*2 +8*1 +CAST(SUBSTRING(身份证号, 7,1) AS INT)*6 +CAST(SUBSTRING(身份证号, 8,1) AS INT)*3 +CAST(SUBSTRING(身份证号, 9,1) AS INT)*7 +CAST(SUBSTRING(身份证号,10,1) AS INT)*9 +CAST(SUBSTRING(身份证号,11,1) AS INT)*10 +CAST(SUBSTRING(身份证号,12,1) AS INT)*5 +CAST(SUBSTRING(身份证号,13,1) AS INT)*8 +CAST(SUBSTRING(身份证号,14,1) AS INT)*4 +CAST(SUBSTRING(身份证号,15,1) AS INT)*2 ) % 11 + 1, 1) WHERE LEN(身份证号)=15 AND SUBSTRING(身份证号,13,3) IN ('999','998','997','996') -- 15位身份证号升级为18位,适用于19xx年出生的公民 UPDATE 员工表 SET 身份证号= SUBSTRING(身份证号,1,6)+'19'+SUBSTRING(身份证号,7,9)+ SUBSTRING('10X98765432', ( CAST(SUBSTRING(身份证号, 1,1) AS INT)*7 +CAST(SUBSTRING(身份证号, 2,1) AS INT)*9 +CAST(SUBSTRING(身份证号, 3,1) AS INT)*10 +CAST(SUBSTRING(身份证号, 4,1) AS INT)*5 +CAST(SUBSTRING(身份证号, 5,1) AS INT)*8 +CAST(SUBSTRING(身份证号, 6,1) AS INT)*4 +1*2 +9*1 +CAST(SUBSTRING(身份证号, 7,1) AS INT)*6 +CAST(SUBSTRING(身份证号, 8,1) AS INT)*3 +CAST(SUBSTRING(身份证号, 9,1) AS INT)*7 +CAST(SUBSTRING(身份证号,10,1) AS INT)*9 +CAST(SUBSTRING(身份证号,11,1) AS INT)*10 +CAST(SUBSTRING(身份证号,12,1) AS INT)*5 +CAST(SUBSTRING(身份证号,13,1) AS INT)*8 +CAST(SUBSTRING(身份证号,14,1) AS INT)*4 +CAST(SUBSTRING(身份证号,15,1) AS INT)*2 ) % 11 + 1, 1) WHERE LEN(身份证号)=15 AND SUBSTRING(身份证号,13,3) NOT IN ('999','998','997','996')